我想model
是CustomerPart實例,並且您已經或多或少地這樣定義它。
public class CustomerPart
{
public int DeliveryRun_Id {get; set;}
public SelectList(or some IEnumerable) DeliveryRun_Id
}
我覺得你的代碼沒有更新數據庫,因爲你使用了錯誤的屬性。第一個lambda表達式應該是model => model.TheAttributeYouWantToUpdate
,在這種情況下是DeliveryRun_Id
。
所以它應該是:
@Html.DropDownListFor(model => model.DeliveryRun_Id, Model.DeliveryRunList)
而不是
它也不是不清楚的地方是這個代碼在控制器內部:
CustomerPart custPart = _custService.Get(custId);
if (DeliveryRunList.HasValue)
{
custPart.DeliveryRun_Id = DeliveryRunList.Value;
}
_custService.Update(custPart);
這樣做的一種常見方式是有兩個同名的方法進行編輯,一個用於HttpGet,一個用於HttpPost,並使用@Html.BeginForm()
在剃刀視圖中進行更新,而不是更新控制器中的信息。
例子:
public ActionResult Edit(int id = 0) {
InvestmentFund Fund = InvestmentFundData.GetFund(id);
return Fund == null ? (ActionResult)HttpNotFound() : View(Fund);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(InvestmentFund Fund)
{
if (ModelState.IsValid)
{
InvestmentFundData.Update(Fund);
return RedirectToAction("List");
}
return View(Fund);
}
在查看
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@* For the attributes of your model *@
@Html.LabelFor ...
@Html.EditorFor ...
@Html.ValidationMessageFor ...
<input type="Submit"m value="Save">
}
聽起來像是你只需要在加載頁面時填充它。獲取客戶城鎮並設置下拉列表的選定索引=城鎮值(假設您對城鎮ID /城鎮文本中的數據和文本字段有約束力) – DGibbs
您是否調試過該語句以確保更新實際發生。 。我認爲,如果你的模型綁定到下拉列表的現有值,那麼它將始終有一個值... – Rikon
請發佈定義DeliveryRunList的代碼。 – ataravati