使用 Ajax 表单进行模型绑定

发布于 2024-09-26 11:06:09 字数 4786 浏览 2 评论 0 原文

我使用 asp.net mvc2 并使用 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>                
<%}%>

,下面是控制器中我的操作方法的签名,

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

当 EmployeeID 和 Todo 变量预计包含表单中的值列表时,我在 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)

i m using asp.net mvc2 and i m submitting a form through ajax using jquery. in this scenario model binding does not work

Here is my View code

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

below is signature of my action method in the controller

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

i find no values in EmployeeID and Todo variables when they are expected to contain list of values from the form. i will appreciate any help and suggestions

Edit one

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

and my method signature is like

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

温柔少女心 2024-10-03 11:06:09

可以看看你的视图代码是什么样子的吗?

它应该看起来像这样:

<%@ 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)

如果你可以将类型设置为 IEnumerable 或者如果你有将其设置为操作方法签名中的 Employee[],但其中之一应该有效。

另请注意,在第一行,您必须确保在 Inherits 属性中包含命名空间,如下所示:System.Web.Mvc.ViewPage>当然,使用您的员工班级的真实姓名。

希望这有帮助。

Is it possible to see what your view code look like?

It should look something like this:

<%@ 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

And your action method signature should look like this:

public ActionResult MeetingTodo(IEnumerable<Employee> Employees,
    FormCollection collection,
    int id)

I don't remember of the top of my head if you can set the type to IEnumerable<Employee> or if you have to set it to Employee[] in the action method signature, but one or the other should work.

Also note that on the first line you have to make sure you include the namespace in the Inherits attribute as such: System.Web.Mvc.ViewPage<IEnumerable<Namespace1.Namespace2.Namespace3.Employee>> and of course use whatever real name your employee class has.

Hope this helps.

你穿错了嫁妆 2024-10-03 11:06:09

我相信您没有获得正确值的原因是您使用 Html.TextAreaFor 的方式错误。代码中的 x 指向您的 Page.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>                
<%}%>

然后在操作声明中按如下方式装饰参数:

public ActionResult MeetingTodo([Bind(Prefix = "employee")]Employee[] employees)

不过,更好、更简洁的方法是这样做。

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

I believe the reason you're not getting the correct values is because you're using Html.TextAreaFor the wrong way. The x in your code points to your Page.Model attribute while your val is not related to that attribute in a way that ASP.NET MVC2 can understand.

You could change your code to look something like this:

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

And then in your action declaration decorate your parameters as following:

public ActionResult MeetingTodo([Bind(Prefix = "employee")]Employee[] employees)

A better and cleaner way would be to do this though.

<% for (int i = 0; i < Model.Count(); ++i) 
{ %>
<%= Html.HiddenFor(m => m[i].SomeAttribute) %>
<div><%= Html.TextAreaFor(m => m[i].SomeTextAttribute) %></div>
<% } %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文