【发布时间】:2016-08-15 05:43:19
【问题描述】:
我想要在SelectDaycshtml 页面中呈现查看页面ViewByDay.cshtml 的员工详细信息。概念是从下拉列表中选择一周中的几天,并在ViewByDay 中检索与那一天相关的信息。为此,我在SelectDay 中使用了@Html.RenderAction("ViewByDay")。但是我收到了这个错误
过程或函数“ViewByDay”需要参数“@Days”,但未提供。
这是我的视图类和控制器的代码 sn-ps:
SelectDay.cshtml
@model CrudMvc.Models.EmpInfoModels
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>Select Employee By Days</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Days, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.DropDownListFor(model => model.Days, (List < SelectListItem >)ViewBag.DayItems, new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.Days, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Select" class="btn btn-default" />
</div>
</div>
}
<div>
@{ Html.RenderAction("ViewByDay"); }
// @{ Html.RenderPartial("_ViewByDay");}
// @Html.Partial("_ViewByDay")
// @Html.ActionLink("Get All Employee Details", "GetAllEmpDetails","Employee") *@
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
ViewByDay.cshtml
@model IList<CrudMvc.Models.EmpInfoModels>
<div>
<h4>ListEmpByDays</h4>
<hr />
<table class="table">
<tr>
<th>Employee Name</th>
<th>Day</th>
<th>Destination</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>@Html.DisplayFor(modelItem => item.EmpName)</td>
<td>@Html.DisplayFor(modelItem => item.Days)</td>
<td>@Html.DisplayFor(modelItem => item.Destination)</td>
</tr>
}
</table>
</div>
<p>@Html.ActionLink("Back to List", "GetAllEmpDetails")</p>
控制器 SelectDay 和 ViewByDay
public ActionResult SelectDay()
{
var days= new List<SelectListItem>();
days.Add(new SelectListItem { Text = "Monday", Value = "Monday" });
days.Add(new SelectListItem { Text = "Tuesday", Value = "Tuesday" });
days.Add(new SelectListItem { Text = "Wednesday", Value = "Wednesday" });
days.Add(new SelectListItem { Text = "Thursday", Value = "Thursday" });
days.Add(new SelectListItem { Text = "Friday", Value = "Friday" });
ViewBag.DayItems = days;
return View();
}
[HttpPost]
public ActionResult SelectDay(string Days)
{
return RedirectToAction("ViewByDay", new {Days = Days});
}
public ActionResult ViewByDay(string days)
{
EmpRepository EmpRepo = new EmpRepository();
ModelState.Clear();
var emps = EmpRepo.ViewByDay(days);
return View(emps);
}
【问题讨论】:
-
从
SelectDay.cshtml中删除@{ Html.RenderAction("ViewByDay"); }- 你有一个表单并且你发布了Days的值,然后重定向到ViewByDaymethod/view。旁注:ViewByDay()方法中的ModelState.Clear();毫无意义(ModelState中没有什么要清除 -
您的代码没有真正意义。如果您想在第一个视图中显示表格,那么您需要一个带有
FormMethod.Get的表单,并且SelectDay()方法需要有一个Days的参数(并且您需要返回模型)。但是通过使用 ajax 根据所选选项更新 DOM,您将获得更好的性能 -
亲爱的朋友。我建议您阅读有关部分视图的信息。然后在 ViewByDay 和 SelectDay 中使用相同的局部视图。否则我认为 Stefan Kert 的答案是正确的。
标签: c# asp.net-mvc asp.net-mvc-4 asp.net-mvc-partialview renderaction