我有一个下拉列表,显示数据库的db.Budgets的Code&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">−</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数据库。没有错误。
我做错了什么?
****编辑****
我已经尝试过放置控制器文本,但是它不起作用:
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Create([Bind(Include = "ClinicalAssetID,AssetTypeID,ProductID,ManufacturerID,ModelID,SupplierID,SerialNo,PurchaseDate,PoNo,Costing,TeamID,StaffID,WarrantyEndDate,InspectionDate,InspectionOutcome,InspectionDocumnets,InspectionDueDate,BudgetCodeID")] ClinicalAsset clinicalAsset)
{
if (ModelState.IsValid)
{
db.ClinicalAssets.Add(clinicalAsset);
await db.SaveChangesAsync();
return RedirectToAction("Index");
}
ViewBag.AssetTypeID = new SelectList(db.AssetTypes, "AssetTypeID", "AssetTypeName", clinicalAsset.AssetTypeID);
ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductName", clinicalAsset.ProductID);
ViewBag.ModelID = new SelectList(db.Models, "ModelID", "ModelName", clinicalAsset.ModelID);
ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ManufacturerID", "ManufacturerName", clinicalAsset.ManufacturerID);
ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName", clinicalAsset.SupplierID);
ViewBag.TeamID = new SelectList(db.Teams, "TeamID", "TeamName", clinicalAsset.TeamID);
ViewBag.StaffID = new SelectList(db.Staffs, "StaffID", "StaffName", clinicalAsset.StaffID);
ViewBag.InspectionOutcomeID = new SelectList(db.InspectionOutcomes, "InspectionOutcomeID", "InspectionOutcomeResult", clinicalAsset.InspectionOutcomeID);
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");
return View(clinicalAsset);
}
****更新****
我发现从控制器中删除异步确实会将整数回发到表中。然后如何使其与异步一起工作?还是如果我不希望由于用户群太少而导致繁重的负载或带宽问题,是否应该完全放弃异步?
[HttpPost]
[ValidateAntiForgeryToken]
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)
{
if (ModelState.IsValid)
{
db.ClinicalAssets.Add(clinicalAsset);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AssetTypeID = new SelectList(db.AssetTypes, "AssetTypeID", "AssetTypeName", clinicalAsset.AssetTypeID);
ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductName", clinicalAsset.ProductID);
ViewBag.ModelID = new SelectList(db.Models, "ModelID", "ModelName", clinicalAsset.ModelID);
ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ManufacturerID", "ManufacturerName", clinicalAsset.ManufacturerID);
ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName", clinicalAsset.SupplierID);
ViewBag.TeamID = new SelectList(db.Teams, "TeamID", "TeamName", clinicalAsset.TeamID);
ViewBag.StaffID = new SelectList(db.Staffs, "StaffID", "StaffName", clinicalAsset.StaffID);
ViewBag.InspectionOutcomeID = new SelectList(db.InspectionOutcomes, "InspectionOutcomeID", "InspectionOutcomeResult", clinicalAsset.InspectionOutcomeID);
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");
return View(clinicalAsset);
}
``````
答案 0 :(得分:1)
您需要将要发布的数据包装成表格:
@using (Html.BeginForm("Search", "YOUR CONTROLLER", FormMethod.Post)) {
<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>
}
您的POST操作需要接受您要发布的模型:
public ActionResult Post(MyModel model)
{
//call a service to save info to database
}
答案 1 :(得分:1)
我发现从控制器中删除异步确实会将整数回发到表中。
[HttpPost]
[ValidateAntiForgeryToken]
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)
{
if (ModelState.IsValid)
{
db.ClinicalAssets.Add(clinicalAsset);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.AssetTypeID = new SelectList(db.AssetTypes, "AssetTypeID", "AssetTypeName", clinicalAsset.AssetTypeID);
ViewBag.ProductID = new SelectList(db.Products, "ProductID", "ProductName", clinicalAsset.ProductID);
ViewBag.ModelID = new SelectList(db.Models, "ModelID", "ModelName", clinicalAsset.ModelID);
ViewBag.ManufacturerID = new SelectList(db.Manufacturers, "ManufacturerID", "ManufacturerName", clinicalAsset.ManufacturerID);
ViewBag.SupplierID = new SelectList(db.Suppliers, "SupplierID", "SupplierName", clinicalAsset.SupplierID);
ViewBag.TeamID = new SelectList(db.Teams, "TeamID", "TeamName", clinicalAsset.TeamID);
ViewBag.StaffID = new SelectList(db.Staffs, "StaffID", "StaffName", clinicalAsset.StaffID);
ViewBag.InspectionOutcomeID = new SelectList(db.InspectionOutcomes, "InspectionOutcomeID", "InspectionOutcomeResult", clinicalAsset.InspectionOutcomeID);
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");
return View(clinicalAsset);
}
答案 2 :(得分:0)
在您的代码中。您以操作方法发送的数据为POST类型。
[HttpPost]
[ValidateAntiForgeryToken] 公共异步任务Create()
您必须在视图页面中定义“ FormMethod”。例如:
@using(Html.BeginForm(“ ActionMethod名称”,“控制器名称”,FormMethod.Post)) {
} ` 希望这对我有帮助。