Note that there are some explanatory texts on larger screens.

plurals
  1. POMultiple forms on mvc4 page, submit one form
    primarykey
    data
    text
    <p>I'm facing an issue with listing multiple forms, incl submit buttons. When clicking the first button it posts correctly, but when I click the second submit button it submits an empty collection... Below is my code:</p> <p>Index.cshtml</p> <pre><code>@model MVCFormsSubmitting.Models.FormsRepository @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } &lt;h2&gt;Index&lt;/h2&gt; @for (int i = 0; i &lt; Model.UserInfo.Count; i++) { using (Html.BeginForm("Index", "Forms", FormMethod.Post, new {name = "Form" + @i})) { &lt;b&gt;@Html.EditorFor(m =&gt; m.UserInfo[i].Name)&lt;/b&gt; &lt;input name="button @i" type="submit" class="left btn btn-primary" value="Ret navne"&gt; &lt;br/&gt; } } </code></pre> <p>Formsrepository.cs</p> <pre><code>namespace MVCFormsSubmitting.Models { public class Info { public int Id { get; set; } public string Name { get; set; } public string Age { get; set; } public string Email { get; set; } public string Phone { get; set; } } public class FormsRepository { public FormsRepository () { this.UserInfo = new List&lt;Info&gt;(); } public List&lt;Info&gt; UserInfo { get; private set; } public async Task Load() { this.UserInfo.Clear(); UserInfo = await LoadUsers(); } public async static Task&lt;List&lt;Info&gt;&gt; LoadUsers() { List&lt;Info&gt; info = new List&lt;Info&gt;(); info.Add(new Info(){ Age = "32,", Email = "mail@mail.com", Name = "John Doe", Phone = "123456749", Id = 0 }); info.Add(new Info() { Age = "36", Email = "exmaple@example.com", Name = "Jane Doe", Phone = "987654321", Id = 1 }); return info; } } } </code></pre> <p>FormsController.cs</p> <pre><code>public class FormsController : Controller { // // GET: /Forms/ public ActionResult Index() { FormsRepository.Load(); return View(FormsRepository); } [HttpPost] public ActionResult Index(FormsRepository text) { return RedirectToAction("Index"); } private static FormsRepository _repository; public static FormsRepository FormsRepository { get { if (_repository == null) { _repository = new FormsRepository(); } return _repository; } } } </code></pre> <p>When setting a breakpoint at the HttpPost action in the Formscontroller you will see when clicking the submit button on the first will send 1 item, but when clicking the second button the item is null ...</p> <p>please help :)</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload