从下拉列表中选择其他文本后,如何在显示文本框时保存与下拉列表相同类型的值?

时间:2016-06-27 20:44:31

标签: c# asp.net-mvc

当我从MVC的下拉列表中选择“其他”值时,我已按照this链接显示文本框。它运作顺利。现在我的问题是将新输入的值从用户保存在数据库的新字段中并将其发送回控制器操作。它不起作用我的视图代码下拉列表和它下面出现的文本框,然后是我的动作和我发送的变量的图片。请帮助我很困惑。

查看

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

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

动作

[HttpPost]
        public ActionResult Create(tbl_Url myURL)
        {
            try
            {

                myURL.IsApproved = "P";
                myURL.UserId = objUserBs.GetALL().Where(x => x.UserEmail == User.Identity.Name).FirstOrDefault().UserId;
                if (ModelState.IsValid)
                {
                    objBs.Insert(myURL);
                    TempData["Msg"] = "Created Successfully";
                    return RedirectToAction("Index");
                }
                else
                {
                    ViewBag.CategoryId = new SelectList(objCatBs.GetALL(), "CategoryId", "CategoryName");//taking data from tbl_Category for the dropdown list in the view, having the values of CategoryId displayed under the name CategoryName
                    ViewBag.UserId = new SelectList(objUserBs.GetALL(), "UserId", "UserEmail");
                    return View("Index");
                }
            }
            catch (Exception e1)
            {
                TempData["Msg"] = "Create Failed " + e1.Message;
                return RedirectToAction("Index");
            }
        }

other1 other2

0 个答案:

没有答案