我想将整个模型传递给控制器。现在它没有任何通过,我不知道为什么。当模型进入此页面时,模型会被部分填充,因此它应至少包含一些信息。
问题:当我点击视图上的按钮时,模型不会传递给控制器。甚至没有填满的部分!
视图(包含部分填充的模型)
@{
ViewBag.Title = "Winkelmand";
Layout = "~/Views/Shared/_Layout3.cshtml";
}
@model IntroSport.Models.Bestel
<br />
<br />
@if (Model.winkelmand.facturen != null)
{
<table style=" width:auto; height:auto; border-style:solid; border-color:black;">
<tr><th>Artikelnummer</th><th>Merk</th><th>Naam</th><th>Prijs</th><th>Aantal</th></tr>
@for (int i = 0; i < Model.winkelmand.facturen.Count; i++)
{
<tr>
<td>@Model.winkelmand.facturen[i].product.ProductID</td>
<td>@Model.winkelmand.facturen[i].product.Merk</td>
<td>@Model.winkelmand.facturen[i].product.Naam</td>
<td>€@Model.winkelmand.facturen[i].product.Prijs</td>
<td >@Model.winkelmand.facturen[i].Aantal</td>
</tr>
}
</table>
}
<br />
<hr />
<b>Totaal: </b>€ @Model.winkelmand.Totale_prijs
<a href="@Url.Action("Klantoverzicht", "Winkelmand", Model)">Bestel!</a>
控制器:
public ActionResult KlantOverzicht(Bestel bestel)
{
return View(bestel);
}
答案 0 :(得分:0)
您无法简单地将整个模型提供到@Url.Action
中,并让它与模型中的每个字段创建链接。
您基本上需要“手动”创建链接!
从您拥有的角度来看,用户似乎没有更改任何信息,并且很可能是您从数据库中提取了您正在显示的信息,因此如果您确实创建了传递模型的链接,则信息将匹配数据库中的数据。
我的建议是,不是将未更改的模型发布到服务器,而是将对象的Id传递到视图中,并让视图从数据库中检索对象。
<a href="@Url.Action("Klantoverzicht", "Winkelmand", new { id = Model.Id })">Bestel!</a>
答案 1 :(得分:0)
您可以使用Model
将<form>
发送给控制器。
或者通过链接发送所有属性并将其收集到新模型中。
<a href="@Url.Action("Klantoverzicht", "Winkelmand",
new {
pId = Model.ProductID,
mark = Model.Merk,
name = Model.Naam,
price = Model.Prijs,
number = Model.Aantal,
})">Bestel!</a>
行动中:
public ActionResult KlantOverzicht(int pId, string mrk, string nam, double price, int number )
{
Bestel model = new Bestel();
model.ProductID = pId;
model.Merk = mark;
model.Naam = name;
model.Prijs = price;
model.Aantal = number;
}