Skip to content Skip to sidebar Skip to footer

Post A Form Array Without Successful

I'm developing an ASP.NET MVC 5 web with C# and .NET Framework 4.5.1. I have this form in a cshtml file: @model MyProduct.Web.API.Models.ConnectBatchProductViewModel @{ Layout

Solution 1:

You need to generate the controls for the collection in a for loop so they are correctly named with indexers (note that property BatchProducts needs to be IList<BatchProductViewModel>

@using (Html.BeginForm("Save", "ConnectBatchProduct", FormMethod.Post))
{
  ....
  <table>
    ....
    @for(int i = 0; i < Model.BatchProducts.Count; i++)
    {
      <tr><td>@Html.TextBoxFor(m => m.BatchProducts[i].Quantity)</td><td>@Html.TextBoxFor(m => m.BatchProducts[i].BatchName)</td><td>
          // add the following to allow for dynamically deleting items in the view
          <inputtype="hidden"name="BatchProducts.Index"value="@i" /><aclass="deleteRow"></a></td></tr>
    }
    ....
  </table>
  ....
}

Then the POST method needs to be

public ActionResult Save(ConnectBatchProductViewModel model){
  ....
}

Edit

Note: Further to your edit, if you want to dynamically add and remove BatchProductViewModel items in he view, you will need to use the BeginCollectionItem helper or a html template as discussed in this answer

The template to dynamically add new items would be

<divid="NewBatchProduct"style="display:none"><tr><td><inputtype="text"name="BatchProducts[#].Quantity"value /></td><td><inputtype="text"name="BatchProducts[#].BatchName"value /></td><td><inputtype="hidden"name="BatchProducts.Index"value ="%"/><aclass="deleteRow"></a></td></tr></div>

Note the dummy indexers and the non-matching value for the hidden input prevents this template posting back.

Then the script to add a new BatchProducts would be

$("#addrow").click(function() {
  var index = (newDate()).getTime(); // unique indexervar clone = $('#NewBatchProduct').clone(); // clone the BatchProducts item// Update the index of the clone
  clone.html($(clone).html().replace(/\[#\]/g, '[' + index + ']'));
  clone.html($(clone).html().replace(/"%"/g, '"' + index  + '"'));
  $("table.order-list").append(clone.html());
});

Solution 2:

In your Post Methode you receive "MyProduct.Web.API.Models.ConnectBatchProductViewModel" as Parameter. Use the existing model for the Post methode.

Why do you want a IEnumerable from your model? there is only one available including the id in the model.

Solution 3:

you can visit this article for complete source code with a video tutorial.

you have to create an action first, from where we can pass the list of object

[HttpGet]
public ActionResult Index()
{
    List<Contact> model = new List<Contact>();
    using (MyDatabaseEntities dc = new MyDatabaseEntities())
    {
        model = dc.Contacts.ToList();
    }
    return View(model);
}

then we need to create a view for that action

@modelList<UpdateMultiRecord.Contact>
@{
    ViewBag.Title = "Update multiple row at once Using MVC 4 and EF ";
}
@using (@Html.BeginForm("Index","Home", FormMethod.Post))
{
    <table><tr><th></th><th>Contact Person</th><th>Contact No</th><th>Email ID</th></tr>
        @for (int i = 0; i < Model.Count; i++)
        {
            <tr><td> @Html.HiddenFor(model => model[i].ContactID)</td><td>@Html.EditorFor(model => model[i].ContactPerson)</td><td>@Html.EditorFor(model => model[i].Contactno)</td><td>@Html.EditorFor(model => model[i].EmailID)</td></tr>
        }
    </table><p><inputtype="submit"value="Save" /></p><pstyle="color:green; font-size:12px;">
        @ViewBag.Message
    </p>
}
 @sectionScripts{
    @Scripts.Render("~/bundles/jqueryval")
 }

and then we have to write code for save the list of object to the database

[HttpPost]
public ActionResult Index(List<Contact> list)
{  
    if (ModelState.IsValid)
    {
        using (MyDatabaseEntities dc = new MyDatabaseEntities())
        {
            foreach (var i in list)
            {
                var c = dc.Contacts.Where(a =>a.ContactID.Equals(i.ContactID)).FirstOrDefault();
                if (c != null)
                {
                    c.ContactPerson = i.ContactPerson;
                    c.Contactno = i.Contactno;
                    c.EmailID = i.EmailID;
                }
            }
            dc.SaveChanges();
        }
        ViewBag.Message = "Successfully Updated.";
        return View(list);
    }
    else
    {
        ViewBag.Message = "Failed ! Please try again.";
        return View(list);
    }
}

Solution 4:

using(Html.BeginForm())
{
  // code here 

}

While to Post form Data all tags must be included form tag.

Solution 5:

Following the principle of DRY, you can create one EditorTemplate for that purpose. Steps:

1- In Views > Shared > Create new folder named (EditorTemplates)

2- Create a view inside your newly created EditorTemplates folder , the view's model should be BatchProductViewModel according to the OP example. Place your code inside the Editor view. No loop or index is required.

An EditorTemplate will act similar to a PartialView for every child entity but in a more generic way.

3- In your parent entity's view, call your Editor : @Html.EditorFor(m => m.BatchProducts)

Not only this provides a more organized views, but also let's you re-use the same editor in other views as well.

Post a Comment for "Post A Form Array Without Successful"