Create a new row in the table using in-line creation capabilities

After developing a basic MVC 5 application, I followed these steps:

  1. Create model (using code first) with 4 properties
  2. In the controller, generate view by scaffold.

Everything seems to be functioning correctly! However, when I click on the create button, it takes me to a new page with the necessary fields for creation. This behavior is default, but I am looking for a way to perform an inline create without leaving the current page. In other words, I want the 'create' action to add a new empty row at the beginning of the table with input elements and checkboxes along with a save button on one side.

Here is my simple model and the UI generated by scaffold:

namespace Emp01.Models
{
    public class Emp
    {
        public string name { get; set; }
        public Boolean checkBox1 { get; set; }
        public Boolean checkBox2 { get; set; }
        public Boolean checkBox3 { get; set; }
    }

    public class EmpDbContext : DbContext
    {
        public EmpDbContext()
            : base("DefaultConnection")
        {

        }
        public DbSet<Emp> Emps { get; set; }
    }
}

This is the view

@{
    ViewBag.Title = "Index";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table class="table">
    <tr>
      // Table column headers....
    </tr>

@foreach (var item in Model) {
    <tr>
        // Display item details
    </tr>
}

</table>

Update

In addition to Patil's suggestion, I made some modifications:

  1. I added the code from the create operation to the index page and changed the form class from vertical to inline.

2. Added a new div with an ID of context that should toggle.

Currently, there are two things missing:

  1. How can I make the toggle function work when clicking on the button?

2. When running the page, I encountered an error in the link code (model => model.name) because it is currently empty. Even though the create page is also empty, I fail to understand why...

Please assist!

<script>
    jQuery(document).ready(function () {
        jQuery(".content").hide();
    });
    $("#addmoret").click(function () {
        $(".content").toggle();
    });
</script>

@* ----- Code Snippet goes here ------ *@


          <div class="content">
              <div class="form-inline">
                  // Form elements...
              </div>
          </div>

Update 2

@Html.LabelFor(model => model.name, htmlAttributes: new { @class = "control-label col-md-2" })

Error Message: 'System.Collections.Generic.IEnumerable' does not contain a definition for 'name' ...

Lastly, I added a button:

<button type='button' id='addmore' class="btn ui-state-default medium" value='Add More Credit ?' style="width: 200px;">Create </button>

Answer №1

After experimenting with a similar concept, I discovered a method that involves using a toggle function to reveal a hidden division upon clicking a button. This approach allows for displaying images in a dynamic manner.

To implement this functionality, you can utilize the following code:

<script>
    jQuery(document).ready(function () {
        jQuery(".content").hide();
    });
    $("#addmorecredit").click(function () {
        $(".content").toggle();
    });
</script>

<button type='button' id='addmorecredit' class="btn ui-state-default medium" value='Add More Credit ?' style="width: 200px;">Add More Credit ?</button>
<br />
<br />
<div id="add-credit-form" class="content">
    <fieldset>
        <legend>Add More Credit</legend>
        <br />
        <div class="form-row form-input">
            <div class="form-label col-md-2">
                <label for="">Amount Paid (@Html.DisplayFor(m => m.SelectedCurrency))<span class="required">*</span></label>
            </div>
            <div class="form-input col-md-10">
                <div class="row">
                    <div class="col-md-3">
                        @Html.TextBoxFor(m => m.AmountPaid)
                        @Html.ValidationMessageFor(m => m.AmountPaid)
                    </div>
                </div>
            </div>
        </div>
        <div class="form-row form-input">
            <div class="form-label col-md-2">
                <label for="">Creditor Name<span class="required">*</span></label>
            </div>
            <div class="form-input col-md-10">
                <div class="row">
                    <div class="col-md-3">
                        @Html.TextBoxFor(m => m.CreditorName)
                        @Html.ValidationMessageFor(m => m.CreditorName)
                    </div>
                </div>
            </div>
        </div>
        <div class="form-row">
            <div class="form-label col-md-2">
                <label for="">Notes<span class="required">*</span></label>
            </div>
            <div class="form-input col-md-10">
                <div class="row">
                    <div class="col-md-3">
                        @Html.TextAreaFor(m => m.Notes)
                        @Html.ValidationMessageFor(m => m.Notes)
                    </div>
                </div>
            </div>
        </div>
        <button id='addcredit' class="btn ui-state-default medium">Add Credit</button>
    </fieldset>
</div>
<br />
<br />
<fieldset>
    <legend>Credit History</legend>
    <div>
        <table>
            <thead>
                <tr>
                    <th>Account Id</th>
                    <th>Creditor Name</th>
                    <th>Amount Credited</th>
                    <th>Banked?</th>
                    <th>Date Credited</th>
                </tr>
            </thead>
            <tbody>
                @foreach (var item in Model.PurchaseHistory)
                {                           
                    <tr>
                        <td>@Html.Raw(Convert.ToString(item.AccountId))</td>
                        <td>@Html.Raw(item.CreditorName)</td>
                        <td>@Html.Raw(Convert.ToString(item.Balance))</td>
                        <td>No</td>
                        <td>@Html.Raw(item.Datetime.ToString("dd/MM/yyyy hh:mm tt"))</td>
                    </tr>
                        }
            </tbody>
        </table>
    </div>
</fieldset>

The Visual Representation :

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for correctly loading all elements on an HTML page before making CSS modifications

This question has been asked several times in the past. I am asking because when I used the on ready callback in jQuery, it did not change the placeholder text of my element "search_input". $( document ).ready(function() { $("#search_input").attr(' ...

Navigation through dots on a single page

Having an issue with my dot navigation and anchor links placement. I want the anchors to be vertically centered in the middle of the section, regardless of window size. Here's what I'm aiming for : For larger windows : https://i.sstatic.net/pK4 ...

Guide to retrieving a specific cell value from a gridview and showcasing it in a textbox with jquery on an asp.net platform

Below is the code for my grid view. It consists of a column labeled "Syllabus" and the next column contains edit and delete buttons. When the Edit button is clicked, a popup appears using jQuery. The popup includes a textbox for the Syllabus. How can I ret ...

Deciphering the Structure of Robot Log File (log.html)

Just starting out with Robot framework and I'm looking to tweak the output of log.html that's generated after running test suites. After examining the source code of log.html, I discovered that this line is responsible for adding the test execut ...

Create a Material UI Text Field that has a type of datetime and can span multiple lines

Is it possible to create a Material UI component of type "datetime-local" that can be displayed on multiple lines while still allowing the date to be edited? The issue arises when the width is too narrow and cuts off the date text. Although the TextField ...

Utilize HTML to resize image to fit within container

I've encountered an issue while working with Adobe Dreamweaver CS5. I added a swap behavior to an image in an html document, expecting it to pop up at its original size and restore on mouse out. However, the swapped image ends up filling the entire sc ...

Making a Cross-Origin Resource Sharing (CORS) request with jQuery utilizing the $

I am currently in the process of building a web application that relies heavily on data from multiple domains. Approximately 90% of the requests made by my application are cross-domain requests. However, I have encountered an issue where I am unable to re ...

Can anyone recommend a tutorial for learning how to achieve this task using jQuery?

has really intrigued me and I'm eager to create something similar because it seems like a fun project. However, I am unsure of what this particular style or technique is called. Any pointers on where to find a tutorial or the name of the approach woul ...

Creating a responsive slider in CSS that matches this specific design

Looking to create a responsive sidebar based on this specific design https://i.sstatic.net/YKy6Z.png https://i.sstatic.net/qwTuJ.png Link to design on Figma Key focus is implementing the "< 4/12 >" functionality and ensuring it is respo ...

Layering one canvas on top of another

Here is the code I am working with. With a JavaScript library, I can draw on the first canvas and then merge it onto the second canvas. My question is, how can I make the first canvas stay in place or float, regardless of where I scroll on the second canv ...

Is it feasible to access an Exchange server in C# without using a domain name?

I am currently developing a web application to access an Exchange Server 2010 in order to retrieve emails from the server. My question is, we do not have a domain name for this server. All I have is its IP address. Is it possible to utilize Exchange Web Se ...

What is the best way to send user credentials to a Sharepoint web service?

In the process of developing an ASP.NET application for SharePoint 2010, I am facing the challenge of accessing SharePoint data within my application. To achieve this, I have opted to utilize a web service with a URL that begins with https. Below is the co ...

Utilizing Jquery Validation plugin for Error messages displayed through Bootstrap 5.x popovers

I'm currently struggling to find the correct solution for integrating the jQuery validation plugin with Bootstrap 5.x version popovers. Can anyone offer assistance? ...

Styling with CSS3: positioning floating divs on top of a non-floated div

Checkout this fiddle here: http://jsfiddle.net/PA5T5/1/ I've got a setup with 3 divs - 2 are floated whereas one is not. The two floating divs are positioned on top of the non-floating div. I'm wondering if this approach is correct or if it&apos ...

Choosing a recently inserted row in jqGrid

After reloading the grid, I am trying to select the newly added row, which is always added at the end. However, it seems impossible to do so after the reload. Is there a reliable way to select the last row after reloading the grid? The current code I have ...

Changing a WAV stream to Opus format with NAudio

I am utilizing the SpeechSynthesizer in .Net to create a WAV stream from a specific string. My next step involves converting this WaveStream into Opus format. The libraries I am working with include: NAudio Opus .Net: https://github.com/JohnACarruthers/ ...

The GMaps API v3 is currently being loaded, but is not displaying on the webpage

Although the maps are supposed to be loading, I'm having trouble actually seeing them appear. Troubleshooting function initialize() { var myLatlng = new google.maps.LatLng(-25.363882,131.044922); var mapOptions = { zoom: 4, center: myLat ...

What steps are needed to connect an Android device to a web service on ServiceStack?

I am facing an issue with my android application trying to connect to a simple HelloWorld C# webservice I created on ServiceStack. Whenever I attempt to connect, my application crashes. Below is the code snippet from Eclipse where I try to access the Servi ...

Utilizing Local Files in a Bootstrap Video Carousel

Currently working on a website for a school project and I am trying to incorporate a carousel of videos. After finding a bootstrap carousel template that played videos linked to a server, I attempted to switch it out with local video files without succes ...

Matching the ending bracket within non-quote characters using Regex

I am currently working on creating a regex pattern to match strings such as 1.) $(Something) 2.) $(SomethingElse, ")") 3.) $(SomethingElse, $(SomethingMore), Bla) 4.) $$(NoMatch) <-- should not match 5.) $$$(ShouldMatch) <-- basically $$ will result ...