Asp.net mvc MVC试图将模型从razor视图传递到控制器
所以我的故事是,我在控制器的帖子中遇到了问题,视图似乎工作正常。发生回发时,tm.BookId为0(应为1),列表计数为0。首先,我将显示模型:Asp.net mvc MVC试图将模型从razor视图传递到控制器,asp.net-mvc,Asp.net Mvc,所以我的故事是,我在控制器的帖子中遇到了问题,视图似乎工作正常。发生回发时,tm.BookId为0(应为1),列表计数为0。首先,我将显示模型: public class TransferModel { public TransferModel() { cbItems = new List<CheckBoxItem>(); } public List<CheckBoxItem> cbItems {get;set;}
public class TransferModel
{
public TransferModel()
{
cbItems = new List<CheckBoxItem>();
}
public List<CheckBoxItem> cbItems {get;set;}
public int BookId;
public class CheckBoxItem
{
public int AttributeId { get; set; }
public string Attribute { get; set; }
public bool Selected { get; set; }
}
}
公共类传输模型
{
公共转让模型()
{
cbItems=新列表();
}
公共列表项{get;set;}
公共国际图书编号;
公共类CheckBoxItem
{
公共int AttributeId{get;set;}
公共字符串属性{get;set;}
已选择公共布尔值{get;set;}
}
}
控制器部分:
public ActionResult AddAttributes(int id = 0)
{
db.transMod.BookId = id;
BookInfo book = db.BookInfoes.Find(id);
var latts = db.BookAtts.ToList();
foreach (BookAtt ba in latts)
{
db.transMod.cbItems.Add(new TransferModel.CheckBoxItem { Attribute = ba.Attribute, AttributeId = ba.BookAttId, Selected = false });
}
List<BookAtt> atInList = book.BookAtts.ToList();
foreach (TransferModel.CheckBoxItem cb in db.transMod.cbItems)
{
if (atInList.Exists(item => item.Attribute == cb.Attribute))
cb.Selected = true;
}
return View(db.transMod);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddAttributes(TransferModel tm)
{
List<BookAtt> atPool = db.BookAtts.ToList();
BookInfo book = db.BookInfoes.Find(tm.BookId);
foreach (TransferModel.CheckBoxItem sel in tm.cbItems)
{
if (sel.Selected)
book.BookAtts.Add(atPool.Find(item1 => item1.Attribute == sel.Attribute));
}
db.SaveChanges();
return RedirectToAction("AddAttributes");
}`enter code here`
public ActionResult AddAttributes(int id=0)
{
db.transMod.BookId=id;
bookinfobook=db.BookInfoes.Find(id);
var latts=db.BookAtts.ToList();
foreach(以拿铁为单位的BookAtt ba)
{
db.transMod.cbItems.Add(新TransferModel.CheckBoxItem{Attribute=ba.Attribute,AttributeId=ba.BookAttId,Selected=false});
}
List=book.BookAtts.ToList();
foreach(db.transMod.cbItems中的TransferModel.CheckBoxItem cb)
{
if(存在(item=>item.Attribute==cb.Attribute))
cb.Selected=true;
}
返回视图(db.transMod);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult AddAttributes(TransferModel tm)
{
List atPool=db.BookAtts.ToList();
bookinfobook=db.BookInfoes.Find(tm.BookId);
foreach(tm.cbItems中的TransferModel.CheckBoxItem选择)
{
如果(选择)
book.BookAtts.Add(atPool.Find(item1=>item1.Attribute==sel.Attribute));
}
db.SaveChanges();
返回重定向操作(“添加属性”);
}`在这里输入代码`
最后一个观点是:
@model BrightStar.Models.TransferModel
@{
ViewBag.Title = "Update Attributes";
}
<h2>Add Attributes</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
<table>
@Html.HiddenFor(model => Model.BookId)
@Html.HiddenFor(model => Model.cbItems)
@foreach (var itm in Model.cbItems)
{
<tr>
<td>@Html.HiddenFor(mo => itm.AttributeId)</td>
<td>@Html.CheckBoxFor(mo => itm.Selected)</td>
<td>@Html.DisplayFor(mo => itm.Attribute)</td>
</tr>
}
</table>
<p>
<input type="submit" value="Save" />
</p>
}
enter code here
@model BrightStar.Models.TransferModel
@{
ViewBag.Title=“更新属性”;
}
添加属性
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.HiddenFor(model=>model.BookId)
@Html.HiddenFor(model=>model.cbItems)
@foreach(Model.cbItems中的var itm)
{
@Html.HiddenFor(mo=>itm.AttributeId)
@CheckBoxFor(mo=>itm.Selected)
@DisplayFor(mo=>itm.Attribute)
}
}
在这里输入代码
模型绑定不会自动发生,项目需要采用特定格式才能绑定到POST操作中的列表属性。退房
请尝试在DOM中签出BookId属性的值以确认它为1,否则它将正常绑定。您应该在帮助中引用模型的属性,以便正确生成控件的名称:
@Html.HiddenFor(model => Model.cbItems)
应该是
@Html.HiddenFor(model => model.cbItems)
就这么简单吗?不,他就在那儿
@using(Html.BeginForm()){
你能在这里发布呈现的Html吗?这没有链接到任何东西。这没有链接到任何东西。在视图中,它是1。