Asp.net mvc 2 使用Ajax表单进行模型绑定

Asp.net mvc 2 使用Ajax表单进行模型绑定,asp.net-mvc-2,model-binding,Asp.net Mvc 2,Model Binding,我使用asp.NETMVC2,并使用jquery通过ajax提交表单。在此场景中,模型绑定不起作用 这是我的查看代码 <%using (Html.BeginForm("MeetingTodo", "OA", FormMethod.Post, new { id = "TaskForm" })) {%><%=Html.Hidden("id",ViewContext.RouteData.Values["id"]) %> <div class="containe

我使用asp.NETMVC2,并使用jquery通过ajax提交表单。在此场景中,模型绑定不起作用

这是我的查看代码

<%using (Html.BeginForm("MeetingTodo", "OA", FormMethod.Post, new { id = "TaskForm" }))
  {%><%=Html.Hidden("id",ViewContext.RouteData.Values["id"]) %>
    <div class="container">
    <%foreach (var val in Model.Distinct())
    { %>
            <div class="grdrow" > 
                 <div class="grdrightcaption" style="width:173px;" ><%=val.setupEmployee.EmployeeName%></div>
                 <div class="grdcells" ><%=Html.TextAreaFor(x => val.Todo, new { maxlength = 200, style="width:300px;" })%> <%=Html.HiddenFor(x => val.EmployeeID)%></div>
                <div class="grdcells" style="width:50px;" ><%=Html.CheckBoxFor(x=>val.Required)%></div>

           </div>

        <%}%>
        </div>
        <br />
        <button type="submit" class="button">save</button>                
<%}%>

val.Todo,新的{maxlength=200,style=“width:300px;”})%%>val.EmployeeID)%%>
val.Required)%>

拯救
下面是我在控制器中的操作方法的签名

public ActionResult MeetingTodo(IEnumerable<int> EmployeeID,IEnumerable<string> Todo, FormCollection collection, int id)
public ActionResult MeetingTodo(IEnumerable EmployeeID、IEnumerable Todo、FormCollection集合、int id)
我在EmployeeID和Todo变量中找不到任何值,因为它们应该包含表单中的值列表。我将感谢任何帮助和建议

编辑一个

<FORM id=TaskForm method=post action=/OA.mvc/MeetingTodo jQuery1286197019171="1"><DIV id=tablecontainer>
<DIV class=grdcaption>
<H2>Tasks</H2></DIV>
<DIV class=grdrow>
<DIV style="WIDTH: 173px" class=grdtopcaption>Participant</DIV>
<DIV style="WIDTH: 303px" class=grdtopcaption>Todo</DIV>
<DIV style="WIDTH: 50px" class=grdtopcaption>Required</DIV></DIV><INPUT id=id value=110 type=hidden name=id> 
<DIV class=container>
<DIV class=grdrow>
<DIV style="WIDTH: 173px" class=grdrightcaption>Muhammad Adeel Zahid</DIV>
<DIV class=grdcells><TEXTAREA style="WIDTH: 300px" rows=2 cols=20 name=[0].Todo maxlength="200">Shahzad</TEXTAREA> <INPUT value=19 type=hidden name=[0].EmployeeID></DIV>
<DIV style="WIDTH: 50px" class=grdcells><INPUT value=true type=checkbox name=[0].Required><INPUT value=false type=hidden name=[0].Required></DIV></DIV>
<DIV class=grdrow>
<DIV style="WIDTH: 173px" class=grdrightcaption>Abdul Samad</DIV>
<DIV class=grdcells><TEXTAREA style="WIDTH: 300px" rows=2 cols=20 name=[1].Todo maxlength="200">Syed</TEXTAREA> <INPUT value=21 type=hidden name=[1].EmployeeID></DIV>
<DIV style="WIDTH: 50px" class=grdcells><INPUT value=true CHECKED type=checkbox name=[1].Required><INPUT value=false type=hidden name=[1].Required></DIV></DIV>
<DIV class=grdrow>
<DIV style="WIDTH: 173px" class=grdrightcaption>M. Kafayat Ullah</DIV>
<DIV class=grdcells><TEXTAREA style="WIDTH: 300px" rows=2 cols=20 name=[2].Todo maxlength="200"> Mansoor</TEXTAREA> <INPUT value=23 type=hidden name=[2].EmployeeID></DIV>
<DIV style="WIDTH: 50px" class=grdcells><INPUT value=true type=checkbox name=[2].Required><INPUT value=false type=hidden name=[2].Required></DIV></DIV>
<DIV class=grdrow>
<DIV style="WIDTH: 173px" class=grdrightcaption>Muhammad Shahzad</DIV>
<DIV class=grdcells><TEXTAREA style="WIDTH: 300px" rows=2 cols=20 name=[3].Todo maxlength="200"> Alioor</TEXTAREA> <INPUT value=26 type=hidden name=[3].EmployeeID></DIV>
<DIV style="WIDTH: 50px" class=grdcells><INPUT value=true type=checkbox name=[3].Required><INPUT value=false type=hidden name=[3].Required></DIV></DIV>
<DIV class=grdrow>
<DIV style="WIDTH: 173px" class=grdrightcaption>Syed Mansoor Ali</DIV>
<DIV class=grdcells><TEXTAREA style="WIDTH: 300px" rows=2 cols=20 name=[4].Todo maxlength="200"> Ali</TEXTAREA> <INPUT value=27 type=hidden name=[4].EmployeeID></DIV>
<DIV style="WIDTH: 50px" class=grdcells><INPUT value=true type=checkbox name=[4].Required><INPUT value=false type=hidden name=[4].Required></DIV></DIV></DIV><BR><BUTTON aria-disabled=false class="button ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role=button type=submit jQuery1286197019171="10"><SPAN class=ui-button-text>save</SPAN></BUTTON> </DIV></FORM>

任务
参与者
待办事项
要求的
穆罕默德·阿德尔·扎希德
沙赫扎德
萨马德
赛义德
卡法亚特·乌拉先生
曼苏尔
穆罕默德·沙赫扎德
阿利奥尔
赛义德·曼苏尔·阿里
阿里

保存
我的方法签名是

public ActionResult MeetingTodo(IEnumerable<int> EmployeeID,IEnumerable<string> Todo, FormCollection collection, int id)
public ActionResult MeetingTodo(IEnumerable EmployeeID、IEnumerable Todo、FormCollection集合、int id)

我认为您没有获得正确值的原因是您使用了错误的
Html.textArea for
。代码中的x指向您的
页面.Model
属性,而您的val与该属性没有ASP.NET MVC2可以理解的关系

您可以将代码更改为如下所示:

<%using (Html.BeginForm("MeetingTodo", "OA", FormMethod.Post, new { id = "TaskForm" }))
{%><%=Html.Hidden("id",ViewContext.RouteData.Values["id"]) %>
<div class="container">
var ix = 0;
<%foreach (var val in Model.Distinct())
{ %>
    <div class="grdrow" > 
        <div class="grdrightcaption" style="width:173px;" ><%=val.setupEmployee.EmployeeName%></div>
        <div class="grdcells" ><%=Html.TextArea(String.Format("employee[{0}].Todo", ix), val.Todo, new { maxlength = 200, style="width:300px;" })%>
            <%=Html.Hidden(String.Format("employee[{0}].EmployeeID", ix), val.EmployeeID)%>
        </div>
        <div class="grdcells" style="width:50px;" >
            <%=Html.CheckBox(String.Format("employee[{0}].Required", ix), val.Required)%>
        </div>
    </div>
<% ++ix 
}%>
</div>
<br />
<button type="submit" class="button">save</button>                
<%}%>
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Employee>>" %>
// Other code not in your example
<%using (Html.BeginForm("MeetingTodo", "OA", FormMethod.Post, new { id = "TaskForm" }))
{%>
<%=Html.Hidden("id",ViewContext.RouteData.Values["id"]) %>
<div class="container">
<% for (int i = 0; i < Model.Count(); ++i)
   { %>
   <div class="grdrow" > 
       <div class="grdrightcaption" style="width:173px;" >
           <%= Model[i].setupEmployee.EmployeeName%>
       </div>
       <div class="grdcells" >
           <%= Html.TextAreaFor(x => x[i].Todo, new { maxlength = 200, style="width:300px;" })%> <%= Html.HiddenFor(x => x[i].EmployeeID)%>
       </div>
       <div class="grdcells" style="width:50px;" >
           <%=Html.CheckBoxFor(x =>  x[i].Required) %>
       </div>
   </div>
<% }%>
</div>
<br />
<button type="submit" class="button">save</button>                
<%}%>
// Other code not in your example
public ActionResult MeetingTodo(IEnumerable<Employee> Employees,
    FormCollection collection,
    int id)
一个更好更干净的方法是这样做

<% for (int i = 0; i < Model.Count(); ++i) 
{ %>
<%= Html.HiddenFor(m => m[i].SomeAttribute) %>
<div><%= Html.TextAreaFor(m => m[i].SomeTextAttribute) %></div>
<% } %>

m[i].SomeAttribute)%%>
m[i].SomeTextAttribute)%%>

是否可以查看视图代码的外观

它应该是这样的:

<%using (Html.BeginForm("MeetingTodo", "OA", FormMethod.Post, new { id = "TaskForm" }))
{%><%=Html.Hidden("id",ViewContext.RouteData.Values["id"]) %>
<div class="container">
var ix = 0;
<%foreach (var val in Model.Distinct())
{ %>
    <div class="grdrow" > 
        <div class="grdrightcaption" style="width:173px;" ><%=val.setupEmployee.EmployeeName%></div>
        <div class="grdcells" ><%=Html.TextArea(String.Format("employee[{0}].Todo", ix), val.Todo, new { maxlength = 200, style="width:300px;" })%>
            <%=Html.Hidden(String.Format("employee[{0}].EmployeeID", ix), val.EmployeeID)%>
        </div>
        <div class="grdcells" style="width:50px;" >
            <%=Html.CheckBox(String.Format("employee[{0}].Required", ix), val.Required)%>
        </div>
    </div>
<% ++ix 
}%>
</div>
<br />
<button type="submit" class="button">save</button>                
<%}%>
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Employee>>" %>
// Other code not in your example
<%using (Html.BeginForm("MeetingTodo", "OA", FormMethod.Post, new { id = "TaskForm" }))
{%>
<%=Html.Hidden("id",ViewContext.RouteData.Values["id"]) %>
<div class="container">
<% for (int i = 0; i < Model.Count(); ++i)
   { %>
   <div class="grdrow" > 
       <div class="grdrightcaption" style="width:173px;" >
           <%= Model[i].setupEmployee.EmployeeName%>
       </div>
       <div class="grdcells" >
           <%= Html.TextAreaFor(x => x[i].Todo, new { maxlength = 200, style="width:300px;" })%> <%= Html.HiddenFor(x => x[i].EmployeeID)%>
       </div>
       <div class="grdcells" style="width:50px;" >
           <%=Html.CheckBoxFor(x =>  x[i].Required) %>
       </div>
   </div>
<% }%>
</div>
<br />
<button type="submit" class="button">save</button>                
<%}%>
// Other code not in your example
public ActionResult MeetingTodo(IEnumerable<Employee> Employees,
    FormCollection collection,
    int id)

//其他代码不在您的示例中
x[i].Todo,新的{maxlength=200,style=“width:300px;”})%%>x[i].EmployeeID)%%>
x[i].必选)%>

拯救 //其他代码不在您的示例中
您的操作方法签名应该如下所示:

<%using (Html.BeginForm("MeetingTodo", "OA", FormMethod.Post, new { id = "TaskForm" }))
{%><%=Html.Hidden("id",ViewContext.RouteData.Values["id"]) %>
<div class="container">
var ix = 0;
<%foreach (var val in Model.Distinct())
{ %>
    <div class="grdrow" > 
        <div class="grdrightcaption" style="width:173px;" ><%=val.setupEmployee.EmployeeName%></div>
        <div class="grdcells" ><%=Html.TextArea(String.Format("employee[{0}].Todo", ix), val.Todo, new { maxlength = 200, style="width:300px;" })%>
            <%=Html.Hidden(String.Format("employee[{0}].EmployeeID", ix), val.EmployeeID)%>
        </div>
        <div class="grdcells" style="width:50px;" >
            <%=Html.CheckBox(String.Format("employee[{0}].Required", ix), val.Required)%>
        </div>
    </div>
<% ++ix 
}%>
</div>
<br />
<button type="submit" class="button">save</button>                
<%}%>
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Employee>>" %>
// Other code not in your example
<%using (Html.BeginForm("MeetingTodo", "OA", FormMethod.Post, new { id = "TaskForm" }))
{%>
<%=Html.Hidden("id",ViewContext.RouteData.Values["id"]) %>
<div class="container">
<% for (int i = 0; i < Model.Count(); ++i)
   { %>
   <div class="grdrow" > 
       <div class="grdrightcaption" style="width:173px;" >
           <%= Model[i].setupEmployee.EmployeeName%>
       </div>
       <div class="grdcells" >
           <%= Html.TextAreaFor(x => x[i].Todo, new { maxlength = 200, style="width:300px;" })%> <%= Html.HiddenFor(x => x[i].EmployeeID)%>
       </div>
       <div class="grdcells" style="width:50px;" >
           <%=Html.CheckBoxFor(x =>  x[i].Required) %>
       </div>
   </div>
<% }%>
</div>
<br />
<button type="submit" class="button">save</button>                
<%}%>
// Other code not in your example
public ActionResult MeetingTodo(IEnumerable<Employee> Employees,
    FormCollection collection,
    int id)
public ActionResult会议待办事项(IEnumerable Employees,
FormCollection集合,
int id)
如果您可以在action方法签名中将类型设置为
IEnumerable
,或者必须将其设置为
Employee[]
,我记不起我的想法了,但其中一个应该可以工作

还要注意,在第一行,您必须确保在Inherits属性中包含名称空间,例如:
System.Web.Mvc.ViewPage
,当然,还要使用employee类拥有的任何真实名称


希望这能有所帮助。

谢谢你的回复,埃米尔。我把foreach换成for。现在EmployeeID和Todo集合包含五个元素,每个元素都等于表单上的控件数(正确)。但问题是,这些集合中的条目对于Todo为null,对于EmployeeID为0。我遗漏了一些东西吗?你能发布你的标记现在看起来怎么样以及你的动作方法签名看起来怎么样吗?我可能可以通过这种方式提供更好的帮助。编辑问题以包含actionresult的标记和签名。plz c编辑1号