处理 MVC 表单提交到数据库
所以我大致遵循音乐商店教程。我在 pg.1 上创建了 StoreManagerController。 54左右。它创建了一个包含“创建”、“删除”、“编辑”等功能的视图。它将一些内容保存到数据库中,即我的 EditFor 控件,但没有其他内容。
我有多个 DropDownListFor 控件,由数据库中的表和 Active Directory 用户数据填充。我不知道如何保存这些。这是我的删节代码。感谢您的帮助。
视图:
<div class="createTopInner">
<div class="editor-label">
@Html.LabelFor(model => model.test.Category)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CategoryId, Model.CategoryItems, "")
@Html.ValidationMessageFor(model => model.test.Category)
</div>
</div>
控制器:
public ActionResult Create()
{
// These four lines get active directory users
ActiveDirectoryModel ads = new ActiveDirectoryModel();
ViewBag.assignedto = ads.FetchContacts();
ViewBag.coassignedto = ads.FetchContacts();
ViewBag.notifyto = ads.FetchContacts();
var model = Populate();
return View(model);
}
[HttpPost]
public ActionResult Create(CreateViewModel model)
{
if (ModelState.IsValid)
{
db.TestItems.AddObject(model.test);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
public CreateViewModel Populate()
{
var model = new CreateViewModel
{
CategoryItems =
from c in new IntraEntities().CategoryItems.ToList()
select new SelectListItem
{
Text = c.Name,
Value = c.ID.ToString()
}
};
return model;
}
型号:
public class CreateViewModel
{
public Intra.Models.TestItem test{ get; set; }
public int CategoryId { get; set; }
public IEnumerable<SelectListItem> CategoryItems { get; set; }
}
So I'm loosely following the Music Store tutorial. I created the StoreManagerController on pg. 54ish. And it created a view with the Create, Deleted, Edit, etc. It's saving some stuff to the database, namely my EditFor controls, but nothing else.
I have multiple DropDownListFor controls, populated by both tables in the database and also Active Directory user data. I'm not sure how to get these to save. Here is my abridged code. Thanks for the help.
View:
<div class="createTopInner">
<div class="editor-label">
@Html.LabelFor(model => model.test.Category)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.CategoryId, Model.CategoryItems, "")
@Html.ValidationMessageFor(model => model.test.Category)
</div>
</div>
Controller:
public ActionResult Create()
{
// These four lines get active directory users
ActiveDirectoryModel ads = new ActiveDirectoryModel();
ViewBag.assignedto = ads.FetchContacts();
ViewBag.coassignedto = ads.FetchContacts();
ViewBag.notifyto = ads.FetchContacts();
var model = Populate();
return View(model);
}
[HttpPost]
public ActionResult Create(CreateViewModel model)
{
if (ModelState.IsValid)
{
db.TestItems.AddObject(model.test);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(model);
}
public CreateViewModel Populate()
{
var model = new CreateViewModel
{
CategoryItems =
from c in new IntraEntities().CategoryItems.ToList()
select new SelectListItem
{
Text = c.Name,
Value = c.ID.ToString()
}
};
return model;
}
Model:
public class CreateViewModel
{
public Intra.Models.TestItem test{ get; set; }
public int CategoryId { get; set; }
public IEnumerable<SelectListItem> CategoryItems { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题似乎是,虽然您的大多数输入映射到
test
上的属性,但CategoryId
却没有。对您的实体模型一无所知,很难说,但我猜测您需要从数据库中检索相应的Category
并将其添加到您的TestItem
实例,然后再坚持它。如果您的TestItem
实例上确实有一个CategoryId
属性,您可以设置它,但我猜您没有这样做,因为否则您会直接使用它(就像您对Category
标签所做的那样),而不是向视图模型添加属性。The problem seems to be that, while most of your inputs map to properties on
test
, theCategoryId
doesn't. Not knowing anything about your entity models, it's difficult to say, but I'd hazard a guess that you need to retrieve the correspondingCategory
from the database and add that to yourTestItem
instance before you persist it. If you do have aCategoryId
property on yourTestItem
instance, you could just set it, but I'm guessing that you don't because otherwise you would have used it directly (as you do for theCategory
label) instead of adding a property to the view model.如果您有权访问它/对存储过程了解很多,那么最好在数据库中使用存储过程,然后在实体中调用它们。它的耦合更加松散,并且更容易进行更改,而无需重新编译代码。
If you have access to it / know much about stored procedures, it is much better to use Store procedures inside the database and then call them within Entity. It's much more loosely coupled and easier to make changes to without recompiling code.