查看到控制器MVC的POST列表集合4
问题描述:
在以下问题上搜索了很多内容之后。最后决定与你分享。 我有一个模型。查看到控制器MVC的POST列表集合4
public class EarlyBirdWeb
{
public string Client { get; set; }
[Display(Name = "Job Name")]
public string JobName { get; set; }
public List<SelectListItem> Reasons { get; set; }
public List<Status> status { get; set; }
public List<ETA> etas { get; set; }
[Display(Name="Call BU")]
public string CallBU { get; set; }
}
我将此模型绑定到MVC视图。我的观点如下。
@model List<EarlyBird.Models.EarlyBirdWeb>
<form method="post">
@Html.AntiForgeryToken()
<div class="row">
<table class="table table-bordered table-inverse">
<tr class="bg-warning">
<th>Client
</th>
<th>Job Name
</th>
<th>Reason</th>
<th>Status</th>
<th>ETA</th>
<th>CallBU?
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@item.Client
@Html.Hidden(@item.Client)
</td>
<td>
@item.JobName
</td>
<td>
@Html.DropDownList("--Select One--", (IEnumerable<SelectListItem>)@item.Reasons)
</td>
<td>
<select>
@foreach (var status in item.status)
{
<option value="@status.StatusName">@status.StatusName</option>
}
</select>
</td>
<td>
<select>
@foreach (var etas in item.etas)
{
<option value="@etas.ETATime">@etas.ETATime</option>
}
</select>
</td>
<td>
@item.CallBU
</td>
</tr>
}
</table>
</div>
<div class="row">
<input type="submit" value="Submit" name="Submit" class="btn btn-warning" />
</div>
</form>
现在,当我在视图中填写所有细节并单击提交按钮时,我在控制器中得到空值。
所以,请帮助我如何让我的控制器上的修改列表集合。
谢谢..
答
下面的代码工作正常,请参见下面的代码
型号:
public class EarlyBirdWeb
{
public string Client { get; set; }
[Display(Name = "Job Name")]
public string JobName { get; set; }
public List<SelectListItem> Reasons { get; set; }
public List<Status> status { get; set; }
public List<ETA> etas { get; set; }
[Display(Name="Call BU")]
public string CallBU { get; set; }
}
控制器
[HttpPost]
public ActionResult Sample(EarlyBirdWeb model)
{
if (ModelState.IsValid)
{
// do your stuff like: save to database and redirect to required page.
}
// If we got this far, something failed, redisplay form
return View(model);
}
查看
@model MvcForums.Models.EarlyBirdWeb
@{
using (Html.BeginForm("Sample", "ControllerName", FormMethod.Post))
{
<label>Client</label>
@Html.TextBoxFor(m => m.Client)
<br/>
<label>JobName</label>
@Html.TextBoxFor(m => m.JobName)
<input type="submit" value="btnSubmit" />
}
}
+0
感谢您的回复。但我的观点将返回EarlyBirdWeb的集合和您的解决方案只有一个记录。你能否提出其他建议? – user2493287
答
尝试使用IEnumerable和确保你rcshtml文件不打破MVC公约的法律(CSHTML文件的名称是方法相同)
@model IEnumerable<EarlyBird.Models.EarlyBirdWeb>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="row">
<table class="table table-bordered table-inverse">
<tr class="bg-warning">
<th>Client
</th>
<th>Job Name
</th>
<th>Reason</th>
<th>Status</th>
<th>ETA</th>
<th>CallBU?
</th>
</tr>
@foreach (var item in Model)
{
<tr>
<td>
@item.Client
@Html.Hidden(@item.Client)
</td>
<td>
@item.JobName
</td>
<td>
@Html.DropDownList("--Select One--", (IEnumerable<SelectListItem>)@item.Reasons)
</td>
<td>
<select>
@foreach (var status in item.status)
{
<option value="@status.StatusName">@status.StatusName</option>
}
</select>
</td>
<td>
<select>
@foreach (var etas in item.etas)
{
<option value="@etas.ETATime">@etas.ETATime</option>
}
</select>
</td>
<td>
@item.CallBU
</td>
</tr>
}
</table>
</div>
<div class="row">
<input type="submit" value="Submit" name="Submit" class="btn btn-warning" />
</div>
}
+0
感谢您的回复。我已经尝试过了。但不工作。 – user2493287
可能[将HTML表格复制到ADO.NET DataTable](http://stackoverflow.com/questions/30094047/post-an-html-table-to-ado-net-datatable) –
您不能使用'foreach'循环来生成收集iteme的表单控件(请参阅这个笨蛋) –