mvc4 asp.net dropDownList插入NULL [英] mvc4 asp.net dropDownList Insert NULL

查看:78
本文介绍了mvc4 asp.net dropDownList插入NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里搜索很多并找到这样的东西来制作下拉列表



这是我的控制器:

这传递了我的数据for dropDownList ...



I search a lot here and find something like this to make dropdown list

this is my controller:
This pass my data for dropDownList...

public ActionResult Create()
  {
      var dba = new WHFMDBContext();
      var query = dba.Categories.Select(c => new { c.Id, c.Name });
      ViewBag.Id = new SelectList(query.AsEnumerable(), "Id", "Name", 3);
      return View();
  }







[HttpPost]
        [InitializeSimpleMembership]
        public ActionResult Create(Profits profits)
        {
            var user = db.UserProfiles.FirstOrDefault(x => x.UserId == WebSecurity.CurrentUserId);
            var profit = new Profits
            {
               Value= profits.Value,
               Description = profits.Description,
               DateInput =profits.DateInput,
               CategoryName =profits.CategoryName,// ???
                User = user,

            };
            db.Profits.Add(profit);
            db.SaveChanges();
            return RedirectToAction("Index");
        }







我的观点:




My View :

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Profits</legend>
            
        <div class="editor-field">
           @Html.DropDownList("Id", (SelectList) ViewBag.Id, "--Select One--") 
            </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.CategoryName.Id)
            @Html.ValidationMessageFor(model => model.CategoryName.Id)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Value)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Value)
            @Html.ValidationMessageFor(model => model.Value)
        </div>





此插入数据到数据库但CategoryName_Id为NULL我缺少什么?

和CategoryName = profits.CategoryName这是利润中类别的外键`public Categories CategoryName {get;组; }`



This insert data to database but CategoryName_Id is NULL what am I missing ?
and CategoryName =profits.CategoryName this is a foreign key to Categories in Profits `public Categories CategoryName { get; set; }`

推荐答案

在我看来,编辑器的问题不在于下拉。下拉菜单是设置Profits.Id,编辑器应该将Profits.CategoryName.Id设置为用户在文本上输入的内容,可能因为您没有向视图发送模型而失败。



尝试向模型发送空利润:

Looks to me like the problem is with the editor not the drop down. the drop down is setting Profits.Id, the editor should be setting the Profits.CategoryName.Id to whatever the user entered on the text, maybe it fails because you are not sending a model to the view.

Try sending an empty Profits to the model:
public ActionResult Create()
  {
      var dba = new WHFMDBContext();
      var query = dba.Categories.Select(c => new { c.Id, c.Name });
      ViewBag.Id = new SelectList(query.AsEnumerable(), "Id", "Name", 3);
      Profits model = new Profits();
      model.CategoryName = new CategoryName();
      return View(model);
  }





也不应该有类别名称ID的下拉列表?



Also should''nt you have the dropdown for the category name id?

@Html.DropDownListFor(model => model.CategoryName.Id,(SelectList) ViewBag.Id, "--Select One--" )


这篇关于mvc4 asp.net dropDownList插入NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆