简体   繁体   English

处理MVC表单提交到数据库

[英]Handling MVC form submission to database

So I'm loosely following the Music Store tutorial. 因此,我大致上遵循“音乐商店”教程。 I created the StoreManagerController on pg. 我在pg上创建了StoreManagerController。 54ish. 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. 然后使用“创建”,“删除”,“编辑”等方法创建了一个视图。它将某些内容保存到数据库中,即我的EditFor控件,但没有其他内容。

I have multiple DropDownListFor controls, populated by both tables in the database and also Active Directory user data. 我有多个DropDownListFor控件,分别由数据库中的两个表以及Active Directory用户数据填充。 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; }
}

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. 如果您可以访问它/对存储过程了解很多,最好在数据库中使用存储过程,然后在Entity中调用它们。 It's much more loosely coupled and easier to make changes to without recompiling code. 它更加松散耦合,并且无需重新编译代码即可更轻松地进行更改。

The problem seems to be that, while most of your inputs map to properties on test , the CategoryId doesn't. 问题似乎是,尽管您的大多数输入都映射到test属性,但CategoryId却没有。 Not knowing anything about your entity models, it's difficult to say, but I'd hazard a guess that you need to retrieve the corresponding Category from the database and add that to your TestItem instance before you persist it. 一无所知,很难说,但我冒昧地猜测您需要从数据库中检索相应的Category并将其添加到TestItem实例中,然后再进行持久化。 If you do have a CategoryId property on your TestItem 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 the Category label) instead of adding a property to the view model. 如果您确实在TestItem实例上具有CategoryId属性,则可以对其进行设置,但是我猜您没有设置它,因为否则您将直接使用它(就像对Category标签所做的那样)而不是添加属性到视图模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM