【问题标题】:Dropdown ids not passing on post method in asp.net mvc 5下拉 ID 未在 asp.net mvc 5 中传递 post 方法
【发布时间】:2017-06-13 09:06:15
【问题描述】:

我正在制作一个用户注册页面。同样,我正在使用性别和角色的下拉列表。但是当我发布数据下拉值时没有通过。这是我的代码。您的及时回复将不胜感激; 这里也是图片enter image description here

//Model class


   public class BEUsers
    {
        public int User_Id { get; set; }
        [Required]
        public string Name { get; set; }
        [DisplayName("Role")]
        [Required]
        public int? Role_Id { get; set; }
        [Required]
        public string UserName { get; set; }
        [Required]
        public string Password { get; set; }
        [DisplayName("Gender")]
        [Required]
        public int? Gender_Id { get; set; }

        public DateTime? CreatedOn { get; set; }

        public int? CreatedBy { get; set; }
        [DisplayName("Upload")]

        public string Imagepath { get; set; }

        public DateTime? ModifiedOn { get; set; }

        public int ModifiedBy { get; set; }

        public HttpPostedFileBase Imagefile { get; set; }

        public IEnumerable<BEUsers> ListUsers { get; set; }

        public IEnumerable<BERoles> ListRoles { get; set; }

        public IEnumerable<BEGender> ListGender { get; set; }
    }

//Action methods
[HttpGet]
        public ActionResult Create()
        {
            BEUsers be = new BEUsers();
            be.ListRoles = blRole.Get();
            be.ListGender = blGender.Get();
            ViewBag.Role = new SelectList(blRole.Get(), "Role_Id", "Role_Desc");
            ViewBag.Gender = new SelectList(blGender.Get(), "Gender_Id", "Gender_Desc");
            return View(be);
        }
        [HttpPost]
        public ActionResult Create(BEUsers be)
        {
            if (ModelState.IsValid)
            {
                if (be.Imagefile != null)
                { 
                    // save the file

                    string fileName = Path.GetFileNameWithoutExtension(be.Imagefile.FileName);
                    string extention = Path.GetExtension(be.Imagefile.FileName);
                    fileName = fileName + DateTime.Now.ToString("yymmssfff") + extention;
                    string path = "~/Images/" + fileName;
                    fileName = Path.Combine(Server.MapPath(path) + fileName);
                    be.Imagefile.SaveAs(fileName);

                    be.Imagepath = fileName;
                    blUser.InsertUser(be);
                    return RedirectToAction("Index");
                }
            }
            return View();
        }
//this is the view
@using (Html.BeginForm("Create", "User", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">

        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })


        <div class="form-group">
            @Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Role_Id, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">

                @*@Html.DropDownList("Role", null, "Select Role",htmlAttributes: new { @class = "form-control" })*@

                @Html.ValidationMessageFor(model => model.Role_Id, "", new { @class = "text-danger" })


            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.UserName, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.UserName, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Gender_Id, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
               @*@Html.EditorFor(model => model.Gender_Id, new { htmlAttributes = new { @class = "form-control" } })*@
                @Html.DropDownList("Gender", null,"Select Gender", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.Gender_Id, "", new { @class = "text-danger" })

            </div>
        </div>

        @*<div class="form-group">
            @Html.LabelFor(model => model.CreatedOn, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CreatedOn, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CreatedOn, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CreatedBy, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.CreatedBy, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.CreatedBy, "", new { @class = "text-danger" })
            </div>
        </div>*@

        <div class="form-group">
            @Html.LabelFor(model => model.Imagepath, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" name="Imagefile" id="fileUpload" class="btn btn-default" required />
            </div>
        </div>


        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create"  class="btn btn-default" />
            </div>
        </div>
    </div>
}

【问题讨论】:

  • 它的@Html.DropDownListFor(m =&gt; m.Gender_Id , (IEnumerable&lt;SelectListItem)ViewBag.Gender ,"Select Gender",.... }) 但使用视图模型(参考this Q/A
  • 感谢斯蒂芬。它正在工作

标签: jquery asp.net asp.net-mvc asp.net-mvc-4


【解决方案1】:

您应该将您的 Html 助手更改为:

 @Html.DropDownList("Gender_Id", null,"Select Gender", htmlAttributes: new { @class = "form-control" })

@Html.DropDownList("Role_Id", null, "Select Role",htmlAttributes: new { @class = "form-control" })

但是,您应该使用强类型帮助器来实现这一点:

@Html.DropDownListFor(m => m.Gender_Id , (IEnumerable<SelectListItem)ViewBag.Gender ,"Select Gender",.... })

【讨论】:

  • 感谢@Jaimin Dave
  • 很高兴为您提供帮助 :)
【解决方案2】:

您的下拉菜单应采用如下强类型:

@Html.DropDownListFor(model => model.Gender_Id, null, "-Select Gender-", new { @class = "form-control" })

干杯!!

【讨论】:

    猜你喜欢
    • 2018-05-19
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 2014-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多