【发布时间】:2014-10-02 11:54:27
【问题描述】:
我遇到了以下问题:
用户访问站点,单击“添加”,然后将其发送回控制器,检索模型并再次发送到视图。内部视图,我检查模型是否不为空
并显示数据。
@if (Model != null)
{
<div id="appInfo">
<table>
<tr>
<th>@Html.DisplayNameFor(x => Model.tytul)</th>
<th>@Html.DisplayNameFor(x => Model.kategoria.nazwa)</th>
<th>@Html.DisplayNameFor(x => Model.liczba_ocen)</th>
<th>@Html.DisplayNameFor(x => Model.avg_ocena)</th>
<th>@Html.DisplayNameFor(x => Model.typ)</th>
</tr>
<tr>
<td>@Model.tytul</td>
<td>@ViewData["kategoria"]</td>
<td>@Model.liczba_ocen</td>
<td>@Model.avg_ocena</td>
<td>@Model.typ</td>
</tr>
</table>
</div>
<div>
@using (Html.BeginForm("Confirm", "Wydawca", new { app = @Model }))
{
<input type="submit" value="Cofirm it" />
}
</div>
在结束按钮“确认它”被创建,一旦你点击它调用确认方法,但应用程序变量始终为空。如果我将它的值设置为除 Model 之外的任何值,它就可以工作。
[HttpPost]
public ActionResult Confirm(aplikacja app)
{
...
}
虽然创建按钮“确认”模型不为空,但我检查了。你碰巧知道出了什么问题吗?
生成的html
<form action="/Wydawca/Confirm?app=Adds.Models.aplikacja" method="post">
<input type="submit" value="Zatwierdź" />
【问题讨论】:
-
form标签渲染的html是什么? (不确定你期望
app = @Model呈现什么) -
Html.BeginForm应该环绕您的所有输入元素,否则没有可发布的内容。 -
既然你不编辑任何数据,为什么还要把整个模型post回来(而不是说将路由参数设置为模型的唯一ID)