下拉列表中的 Asp.net MVC 多个字段无法回发到数据库

我有一个下拉列表,显示数据库中 db.Budgets 的代码和 BudgetCodename。


视图片段如下所示:


 @using (Html.BeginForm())

    {

        @Html.AntiForgeryToken()



        <div class="col-md-6">

            <div class="panel panel-default">

                <div class="panel-heading">

                    <div class="panel-btns">

                        <a href="" class="minimize">&minus;</a>

                    </div>

                    <h4 class="panel-title">Block Styled Form</h4>

                    <p>This is an example of form with block styled label.</p>

                </div>

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

                <div class="panel-body">

                    <div class="row">

                        <div class="col-sm-6">

                            <div class="form-group">

                               @Html.LabelFor(model => model.BudgetCodeID, "BudgetCode:", new { @class = "control-label" })

                               @Html.DropDownListFor(m => m.BudgetCodeID, (SelectList)ViewBag.BudgetsList, new { @class = "form-control" })

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

                                   </div>


}

控制器看起来像这样:


 public ActionResult Create()

        {



            var Budgets = (from m in db.BudgetCodes

             select new SelectListItem {

              Text = m.Code + "| " + m.BudgetCodeName,

              Value = m.BudgetCodeID.ToString()

          });


        ViewBag.BudgetsList = new SelectList(Budgets, "Value", "Text");


}


下拉菜单看起来不错,并显示代码和预算代码名称。


但是该框不会将数据回发到 sql 数据库。不会产生任何错误。


我做错了什么?


江户川乱折腾
浏览 137回答 3
3回答

繁星淼淼

您需要将要发布的数据包装到表单中:@using (Html.BeginForm("Search", "YOUR CONTROLLER", FormMethod.Post)) {&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; <div class="form-group">&nbsp; &nbsp; &nbsp; &nbsp; @Html.LabelFor(model => model.BudgetCodeID, "BudgetCode:", new { @class = "control-label" })&nbsp; &nbsp; &nbsp; &nbsp; @Html.DropDownListFor(m => m.BudgetCodeID, (SelectList)ViewBag.BudgetsList, new { @class = "form-control" })&nbsp; &nbsp; &nbsp; &nbsp; @Html.ValidationMessageFor(model => model.BudgetCodeID, "", new { @class = "text-danger" })&nbsp; &nbsp; </div>}&nbsp;您的 POST 操作需要接受您发布的模型:public ActionResult Post(MyModel model){&nbsp; &nbsp; &nbsp;//call a service to save info to database}

临摹微笑

我发现从控制器中删除异步确实会将整数回发到表中。[HttpPost]&nbsp; &nbsp; &nbsp; &nbsp; [ValidateAntiForgeryToken]&nbsp; &nbsp; &nbsp; &nbsp; public ActionResult Create([Bind(Include = "ClinicalAssetID,AssetTypeID,ProductID,ManufacturerID,ModelID,SupplierID,SerialNo,PurchaseDate,PoNo,Costing,TeamID,StaffID,WarrantyEndDate,InspectionDate,InspectionOutcome,InspectionDocumnets,InspectionDueDate, BudgetCodeID")] ClinicalAsset clinicalAsset)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (ModelState.IsValid)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.ClinicalAssets.Add(clinicalAsset);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; db.SaveChanges();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return RedirectToAction("Index");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewBag.AssetTypeID = new SelectList(db.AssetTypes, "AssetTypeID", "AssetTypeName", clinicalAsset.AssetTypeID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductName", clinicalAsset.ProductID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewBag.ModelID = new SelectList(db.Models, "ModelID", "ModelName", clinicalAsset.ModelID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ManufacturerID", "ManufacturerName", clinicalAsset.ManufacturerID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName", clinicalAsset.SupplierID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewBag.TeamID = new SelectList(db.Teams, "TeamID", "TeamName", clinicalAsset.TeamID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewBag.StaffID = new SelectList(db.Staffs, "StaffID", "StaffName", clinicalAsset.StaffID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewBag.InspectionOutcomeID = new SelectList(db.InspectionOutcomes, "InspectionOutcomeID", "InspectionOutcomeResult", clinicalAsset.InspectionOutcomeID);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var Budgets = (from m in db.BudgetCodes&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;select new SelectListItem&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Text = m.Code + " | " + m.BudgetCodeName,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Value = m.BudgetCodeID.ToString()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;});&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ViewBag.BudgetsList = new SelectList(Budgets, "Value", "Text");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return View(clinicalAsset);&nbsp; &nbsp; &nbsp; &nbsp; }

桃花长相依

在你的代码中。您在操作方法中发送的数据属于 POST 类型。&nbsp;&nbsp;&nbsp;[HttpPost][ValidateAntiForgeryToken] 公共异步任务创建()您必须在视图页面中定义“FormMethod”。例如:@using (Html.BeginForm("ActionMethod 名称", "控制器名称", FormMethod.Post)) {} ` 我希望这对我有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP