ASP.Net 具有多个模型的 MVC
ASP.Net MVC with Multiple Models
查看:
@model MyApp.Models.Activity
@using (Html.BeginForm("Activity_Details", "Activity", FormMethod.Post))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.ActivityName)
@Html.HiddenFor(model => model.ActDate)
@Html.HiddenFor(model => model.DateAccomplished)
@Html.HiddenFor(model => model.Participants)
<div class="body-header">
<div>
<h2>Activity Information</h2>
</div>
<br />
<div class="buttons">
<input type="submit" name="submitButton" value="Save" class="button save icon" />
<a class="button" href="@Url.Action("Activity_List", "Activity")"><span class="back icon"></span>Back</a>
</div>
</div>
<br />
<div class="content-wrapper">
<div class="sub-content">
<div class="item">
<span>@Html.DisplayNameFor(model => model.ActivityName)</span>
@Html.DisplayFor(model => model.ActivityName)
</div>
<div class="item">
<span>@Html.DisplayNameFor(model => model.ActDate)</span>
@Html.DisplayFor(model => model.ActDate)
</div>
<div class="item">
<span>@Html.DisplayNameFor(model => model.DateAccomplished)</span>
@Html.DisplayFor(model => model.DateAccomplished)
</div>
</div>
<div class="sub-content">
<table cellspacing="0">
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Actual Amount</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Participants)
{
<tr>
<td>@item.LastName</td>
<td>@item.FirstName</td>
<td>@item.Amount</td>
</tr>
}
</tbody>
</table>
</div>
</div>
}
控制器:
[HttpPost()]
public ActionResult Activity_Details(string submitButton, Activity model)
{
//AFTER CLICKING THE SUBMIT BUTTON
//model.Participants doesnt retained its values
return View(model);
}
public class Activity
{
public string ActivityName { get; set; }
public DateTime ActDate { get; set; }
public DateTime DateAccomplished { get; set; }
public virtual IEnumerable<Participant> Participants { get; set; }
}
public class Participant
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string Amount { get; set; }
}
我有这些模型 Activity 和参与者,它们将显示在一个视图中,参与者模型将迭代创建一个 table。
我现在的问题是我需要在单击提交按钮后更新每个参与者的 "Amount",现在发生的是当模型传回 ActionResult 时...参与者模型为空且仅Activity 模型有数据。
您需要的是一种叫做列表绑定的东西。这个:
@foreach (var item in Model.Participants)
{
<tr>
<td>@item.LastName</td>
<td>@item.FirstName</td>
<td>@item.Amount</td>
</tr>
}
没有发回任何数据。尝试:
@for( int i = 0; i < Model.Participants.Count(); ++i)
{
@Html.HiddenFor(m => m.Participants[i].LastName)
@Html.HiddenFor(m => m.Participants[i].FirstName)
@Html.HiddenFor(m => m.Participants[i].Amount)
}
确保 i
是连续的且递增的顺序,默认活页夹将很乐意将其拾取。
您可以应用其他方法,但这是最简单的方法。
这里有很多错误。首先,您不能对 IEnumerable 进行模型绑定,因为模型绑定器不知道要实例化哪种实际对象来支持它。使用 List<Participant>
或 Collection<Participant>
。
其次,您不能将 HiddenFor 与集合或 IEnumerable 一起使用。你只能在那里放一个谨慎的类型。您需要将列表中的每个参与者写入单独的 HiddenFor()
。无论如何,这可能毫无意义,因为在大多数情况下,无论如何您在服务器上都有此信息,因此没有理由 post 将其返回服务器。特别是在这里,您正在显示数据,而不是更新数据...
您只需要 post 如果用户可以更改数据,或者 select 它(在这种情况下,您只是 posting selection机制,比如Id)
查看:
@model MyApp.Models.Activity
@using (Html.BeginForm("Activity_Details", "Activity", FormMethod.Post))
{
@Html.ValidationSummary(true)
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.ActivityName)
@Html.HiddenFor(model => model.ActDate)
@Html.HiddenFor(model => model.DateAccomplished)
@Html.HiddenFor(model => model.Participants)
<div class="body-header">
<div>
<h2>Activity Information</h2>
</div>
<br />
<div class="buttons">
<input type="submit" name="submitButton" value="Save" class="button save icon" />
<a class="button" href="@Url.Action("Activity_List", "Activity")"><span class="back icon"></span>Back</a>
</div>
</div>
<br />
<div class="content-wrapper">
<div class="sub-content">
<div class="item">
<span>@Html.DisplayNameFor(model => model.ActivityName)</span>
@Html.DisplayFor(model => model.ActivityName)
</div>
<div class="item">
<span>@Html.DisplayNameFor(model => model.ActDate)</span>
@Html.DisplayFor(model => model.ActDate)
</div>
<div class="item">
<span>@Html.DisplayNameFor(model => model.DateAccomplished)</span>
@Html.DisplayFor(model => model.DateAccomplished)
</div>
</div>
<div class="sub-content">
<table cellspacing="0">
<thead>
<tr>
<th>Last Name</th>
<th>First Name</th>
<th>Actual Amount</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model.Participants)
{
<tr>
<td>@item.LastName</td>
<td>@item.FirstName</td>
<td>@item.Amount</td>
</tr>
}
</tbody>
</table>
</div>
</div>
}
控制器:
[HttpPost()]
public ActionResult Activity_Details(string submitButton, Activity model)
{
//AFTER CLICKING THE SUBMIT BUTTON
//model.Participants doesnt retained its values
return View(model);
}
public class Activity
{
public string ActivityName { get; set; }
public DateTime ActDate { get; set; }
public DateTime DateAccomplished { get; set; }
public virtual IEnumerable<Participant> Participants { get; set; }
}
public class Participant
{
public string LastName { get; set; }
public string FirstName { get; set; }
public string Amount { get; set; }
}
我有这些模型 Activity 和参与者,它们将显示在一个视图中,参与者模型将迭代创建一个 table。
我现在的问题是我需要在单击提交按钮后更新每个参与者的 "Amount",现在发生的是当模型传回 ActionResult 时...参与者模型为空且仅Activity 模型有数据。
您需要的是一种叫做列表绑定的东西。这个:
@foreach (var item in Model.Participants)
{
<tr>
<td>@item.LastName</td>
<td>@item.FirstName</td>
<td>@item.Amount</td>
</tr>
}
没有发回任何数据。尝试:
@for( int i = 0; i < Model.Participants.Count(); ++i)
{
@Html.HiddenFor(m => m.Participants[i].LastName)
@Html.HiddenFor(m => m.Participants[i].FirstName)
@Html.HiddenFor(m => m.Participants[i].Amount)
}
确保 i
是连续的且递增的顺序,默认活页夹将很乐意将其拾取。
您可以应用其他方法,但这是最简单的方法。
这里有很多错误。首先,您不能对 IEnumerable 进行模型绑定,因为模型绑定器不知道要实例化哪种实际对象来支持它。使用 List<Participant>
或 Collection<Participant>
。
其次,您不能将 HiddenFor 与集合或 IEnumerable 一起使用。你只能在那里放一个谨慎的类型。您需要将列表中的每个参与者写入单独的 HiddenFor()
。无论如何,这可能毫无意义,因为在大多数情况下,无论如何您在服务器上都有此信息,因此没有理由 post 将其返回服务器。特别是在这里,您正在显示数据,而不是更新数据...
您只需要 post 如果用户可以更改数据,或者 select 它(在这种情况下,您只是 posting selection机制,比如Id)