2013-05-27 128 views
2

我在列出多个表单时遇到问题,包括提交按钮。 当点击第一个按钮,它正确的帖子,但是当我点击第二个提交按钮将其提交一个空集... 下面是我的代码:mvc4页面上的多个表单,提交一个表单

Index.cshtml

@model MVCFormsSubmitting.Models.FormsRepository 
@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Index</h2> 
@for (int i = 0; i < Model.UserInfo.Count; i++) 
{ 
    using (Html.BeginForm("Index", "Forms", FormMethod.Post, new {name = "Form" + @i})) 
    { 
     <b>@Html.EditorFor(m => m.UserInfo[i].Name)</b> 
     <input name="button @i" type="submit" class="left btn btn-primary" value="Ret navne"> 
     <br/> 
    } 
} 

Formsrepository.cs

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<Info>(); 
    } 

    public List<Info> UserInfo { get; private set; } 

    public async Task Load() 
    { 
     this.UserInfo.Clear(); 
     UserInfo = await LoadUsers(); 

    } 

    public async static Task<List<Info>> LoadUsers() 
    { 
     List<Info> info = new List<Info>(); 

     info.Add(new Info(){ 
     Age = "32,", 
     Email = "[email protected]", 
     Name = "John Doe", 
     Phone = "123456749", 
     Id = 0 

     }); 

     info.Add(new Info() 
     { 
      Age = "36", 
      Email = "[email protected]", 
      Name = "Jane Doe", 
      Phone = "987654321", 
      Id = 1 
     }); 

     return info; 
    } 
    } 
} 

FormsController.cs

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; 
    } 
    } 
} 

当在Formscontroller的HttpPost动作设置断点,你会看到,当点击提交的第一个按钮就会发出1项,但点击第二个按钮的产品空当......

请帮助:)

+0

你有没有注意到,每提交和表格具有相同的ID?还有一件事'按钮@i'在词语之间留有空白!看一看,让我知道! – Fals

回答

5

我做了一些更改,以使此代码工作!

1)改变控制器/视图呈现每个的UserInfo作为强类型局部视图:

// This method will receive an info to process, so the model binder will build the model back in the server correctly 
[HttpPost] 
public ActionResult Index(Info userInfo) 
{ 
    return RedirectToAction("Index"); 
} 

// This method will render a partial view for a user info 
[ChildActionOnly] 
public ActionResult GetUserInfo(Info userInfo) 
{ 
    return PartialView("_UserInfo", userInfo); 
} 

2)更改视图呈现为在储存库中的每个用户信息的局部视图:

@model MVCFormsSubmitting.Models.FormsRepository 
@{ 
    ViewBag.Title = "Index"; 
    Layout = "~/Views/Shared/_Layout.cshtml"; 
} 

<h2>Index</h2> 
@for (int i = 0; i < Model.UserInfo.Count; i++) 
{ 
    {Html.RenderAction("GetUserInfo", Model.UserInfo[i]);} 
} 

3)创建一个局部视图来渲染的用户信息“_UserInfo”:

@model MVCFormsSubmitting.Models.Info 

@using (Html.BeginForm("Index", "Forms", FormMethod.Post, new { id = "Form" + @Model.Id, name = "Form" + @Model.Id })) 
{ 
    <b>@Html.EditorFor(m => m.Name)</b> 
    <input id="[email protected]" name="[email protected]" type="submit" class="left btn btn- primary" value="Ret navne"> 
    <br/> 
} 
+0

我会喜欢把这个回答的标记放在这个上,但不幸的是我仍然是一样的: ( –

+0

你可以发布一条生成的HTML的吗?有什么问题! – Fals

+0

<形式行动=“/表格” ID =“Form0”方法=“邮报” NAME =“Form0”><输入类=“文本框单行 “ID = ”UserInfo_0__Name“ 名称= ”的UserInfo [0]请将.Name“ 类型= ”文本“ 的值= ”John Doe的“/> <输入的ID = ”button_(0)“ 名称=” button_(0 )“type =”submit“c姑娘= “左BTN BTN-主要” 值= “RET navne”>

-2

试试这个:

.... 使用(Html.BeginForm( “指数”, “表单”,FormMethod.Post,新{名称= “表” + i.ToString()})) ...


+0

都能跟得上..不起作用:( –

+0
+0

如果你想使用Javascript而不是Jquery使用这个: User907863

相关问题