C# 从Razor视图提交表单时,模型始终为空
我正在开发一个C# 从Razor视图提交表单时,模型始终为空,c#,asp.net-mvc-4,razor,C#,Asp.net Mvc 4,Razor,我正在开发一个ASP.netmvc4应用程序。我的控制器中有一个名为currency.cshtml的视图和两个方法: [HttpGet] public ActionResult Currency() { IEnumerable<Currency> model = unitOfWork.CurrencyRepository.GetAll(); return View(model); } [HttpPost]
ASP.netmvc4
应用程序。我的控制器中有一个名为currency.cshtml
的视图和两个方法:
[HttpGet]
public ActionResult Currency()
{
IEnumerable<Currency> model = unitOfWork.CurrencyRepository.GetAll();
return View(model);
}
[HttpPost]
public ActionResult Currency(IEnumerable<Currency> model)
{
var test = model;
[HttpGet]
公共行动结果货币()
{
IEnumerable model=unitOfWork.CurrencyRepository.GetAll();
返回视图(模型);
}
[HttpPost]
公共行动结果货币(IEnumerable模型)
{
var检验=模型;
这一观点本身是:
@model IEnumerable<MyProject.Models.Currency>
@using (Html.BeginForm(new { ReturnUrl = ViewBag.ReturnUrl }))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<table>
<thead>
<tr>
<th rowspan="2">Code</th>
<th rowspan="2">Currency</th>
<th rowspan="2">Fixing</th>
<th colspan="2">PayDesk</th>
<th colspan="2">NoDesk</th>
</tr>
<tr>
<th>Buy</th>
<th>Sell</th>
<th>Buy</th>
<th>Sell</th>
</tr>
</thead>
<tbody>
@foreach (var item in Model)
{
<tr>
<td>@Html.HiddenFor(modelItem => item.CurrencyID)
@Html.TextBoxFor(modelItem => item.Code)</td>
<td>@Html.TextBoxFor(modelItem => item.CurrencyName)</td>
<td>@Html.TextBoxFor(modelItem => item.FixingRate)</td>
<td>@Html.TextBoxFor(modelItem => item.PayDeskBuy)</td>
<td>@Html.TextBoxFor(modelItem => item.PayDeskSell)</td>
<td>@Html.TextBoxFor(modelItem => item.NoDeskBuy)</td>
<td>@Html.TextBoxFor(modelItem => item.NoDeskSell)</td>
</tr>
}
</tbody>
</table>
<input type="submit" id="form-submit-button" value="Save" />
}
@model IEnumerable
@使用(Html.BeginForm(new{ReturnUrl=ViewBag.ReturnUrl}))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
代码
通货
固定
付款台
诺德斯克
购买
卖
购买
卖
@foreach(模型中的var项目)
{
@Html.HiddenFor(modeleItem=>item.CurrencyID)
@Html.TextBoxFor(modeleItem=>item.Code)
@Html.TextBoxFor(modelItem=>item.CurrencyName)
@Html.TextBoxFor(modeleItem=>item.FixingRate)
@Html.TextBoxFor(modelItem=>item.PayDeskBuy)
@Html.TextBoxFor(modelItem=>item.PayDeskSell)
@Html.TextBoxFor(modeleItem=>item.NoDeskBuy)
@Html.TextBoxFor(modeleItem=>item.NoDeskSell)
}
}
问题是,我在
[HttpPost]公共操作结果货币(IEnumerable model)
中有一个断点,我可以看到当我提交表单时,我得到了这个断点,但是模型总是空的。一切看起来都很直截了当,我就是看不到哪里出了问题,所以我无法取回数据。当我用加载表单时[HttpGet]
方法一切正常,我看到了数据。尝试用绑定for
循环,而不是foreach
@for(int i=0;i<Model.Count;i++)
{
<td>@Html.HiddenFor(modelItem => Model[i].CurrencyID)
@Html.TextBoxFor(modelItem => Model[i].Code)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].CurrencyName)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].FixingRate)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].PayDeskBuy)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].PayDeskSell)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].NoDeskBuy)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].NoDeskSell)</td>
}
@for(int i=0;i模型[i].CurrencyID)
@Html.TextBoxFor(modelItem=>Model[i].Code)
@Html.TextBoxFor(modelItem=>Model[i].CurrencyName)
@Html.TextBoxFor(modelItem=>Model[i].FixingRate)
@Html.TextBoxFor(modelItem=>Model[i].PayDeskBuy)
@Html.TextBoxFor(modelItem=>Model[i].PaydesSell)
@Html.TextBoxFor(modelItem=>Model[i].NoDeskBuy)
@Html.TextBoxFor(modelItem=>Model[i].NoDeskSell)
}
尝试使用
for
循环绑定,而不是foreach
@for(int i=0;i<Model.Count;i++)
{
<td>@Html.HiddenFor(modelItem => Model[i].CurrencyID)
@Html.TextBoxFor(modelItem => Model[i].Code)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].CurrencyName)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].FixingRate)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].PayDeskBuy)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].PayDeskSell)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].NoDeskBuy)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].NoDeskSell)</td>
}
@for(int i=0;i模型[i].CurrencyID)
@Html.TextBoxFor(modelItem=>Model[i].Code)
@Html.TextBoxFor(modelItem=>Model[i].CurrencyName)
@Html.TextBoxFor(modelItem=>Model[i].FixingRate)
@Html.TextBoxFor(modelItem=>Model[i].PayDeskBuy)
@Html.TextBoxFor(modelItem=>Model[i].PaydesSell)
@Html.TextBoxFor(modelItem=>Model[i].NoDeskBuy)
@Html.TextBoxFor(modelItem=>Model[i].NoDeskSell)
}
尝试在模型上使用
循环和索引器
:
@for(int i=0;i<Model.Count();i++)
{
<td>@Html.HiddenFor(modelItem => item.CurrencyID)
@Html.TextBoxFor(modelItem => Model[i].Code)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].CurrencyName)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].FixingRate)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].PayDeskBuy)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].PayDeskSell)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].NoDeskBuy)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].NoDeskSell)</td>
}
在你看来:
@model List<MyProject.Models.Currency>
@型号列表
如果您想了解有关列表绑定的更多详细信息,我建议您阅读以下文章:
尝试在模型上使用循环和索引器
:
@for(int i=0;i<Model.Count();i++)
{
<td>@Html.HiddenFor(modelItem => item.CurrencyID)
@Html.TextBoxFor(modelItem => Model[i].Code)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].CurrencyName)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].FixingRate)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].PayDeskBuy)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].PayDeskSell)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].NoDeskBuy)</td>
<td>@Html.TextBoxFor(modelItem => Model[i].NoDeskSell)</td>
}
在你看来:
@model List<MyProject.Models.Currency>
@型号列表
如果您想了解有关列表绑定的更多详细信息,我建议您阅读以下文章:
为了能够列出对象列表的所有值,并能够将它们提交给POST操作,我遇到了一个奇怪的问题,即我在POST操作中总是提交一个空值。因此,我将razor视图页面顶部的IEnumerable
列表更改为列表
,并且我没有使用@foreach()
,我使用了带有索引的@for()
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>@Html.DisplayFor(modelItem => Model[i].UserId)</td>
@Html.HiddenFor(modelItem => Model[i].UserId)
<td>@Html.CheckBoxFor(modelItem => Model[i].isLead)</td>
</tr>
}
for(int i=0;iModel[i].UserId)
@Html.HiddenFor(modeleItem=>Model[i].UserId)
@CheckBoxFor(modelItem=>Model[i].isLead)
}
为了能够列出对象列表的所有值,并能够将它们提交给POST操作,我遇到了一个奇怪的问题,在POST操作中总是提交一个空值。因此,我将razor视图页面顶部的IEnumerable
列表更改为列表
,并且我没有使用@foreach()
,我使用了带有索引的@for()
@for (int i = 0; i < Model.Count(); i++)
{
<tr>
<td>@Html.DisplayFor(modelItem => Model[i].UserId)</td>
@Html.HiddenFor(modelItem => Model[i].UserId)
<td>@Html.CheckBoxFor(modelItem => Model[i].isLead)</td>
</tr>
}
for(int i=0;iModel[i].UserId)
@Html.HiddenFor(modeleItem=>Model[i].UserId)
@CheckBoxFor(modelItem=>Model[i].isLead)
}
它就像一件小事,但很难理解这两个循环的区别来自何处。+1作为参考,这实际上揭示了为什么for
循环有效,而不仅仅是“this works”或“try this”答案。无法使用[]应用索引对于“System.Collections.Generic.IEnumerable”类型的表达式有什么想法吗?我无法使它与IEnumerable
一起工作。我不知道为什么-只是更改为List
也不起作用,我必须同时使用-List
和@for()
以使其正常工作。在您当前的示例中,我发现使用时出现了一个错误,因为这是一件小事,但很难理解这两个循环的区别来自何处。+1作为参考,这实际上说明了for
循环为什么有效,而不仅仅是“this works”或“try this”答案。无法将[]索引应用于“System.Collections.Generic.IEnumerable”类型的表达式。有什么想法吗?我无法使它与IEnumerable
一起工作。我不确定原因-只是更改为List
也不起作用,我必须同时使用-List
和@for()
以使其正常工作。在您当前的示例中,我发现使用