C# 下拉列表不显示以前选择的内容&;保存的
客户可以查看他们的客户详细信息页面,他们可以在其中更改预记录的交付运行(如果他们也愿意的话)。我有一个包含交付运行城镇的下拉列表:C# 下拉列表不显示以前选择的内容&;保存的,c#,asp.net-mvc,C#,Asp.net Mvc,客户可以查看他们的客户详细信息页面,他们可以在其中更改预记录的交付运行(如果他们也愿意的话)。我有一个包含交付运行城镇的下拉列表: <div class="editor-label">@Html.DropDownListFor(model => model.DeliveryRunList, Model.DeliveryRunList)</div> 谢谢我想模型是一个CustomerPart实例,您或多或少是这样定义的 public class CustomerPa
<div class="editor-label">@Html.DropDownListFor(model => model.DeliveryRunList, Model.DeliveryRunList)</div>
谢谢我想
模型
是一个CustomerPart实例,您或多或少是这样定义的
public class CustomerPart
{
public int DeliveryRun_Id {get; set;}
public SelectList(or some IEnumerable) DeliveryRun_Id
}
我觉得你的代码没有更新数据库,因为你使用了错误的属性。第一个lambda表达式应该是model=>model。您希望更新属性,在本例中为DeliveryRun\u Id
因此,它应该是:
@Html.DropDownListFor(model => model.DeliveryRun_Id, Model.DeliveryRunList)
而不是
@Html.DropDownListFor(model => model.DeliveryRunList, Model.DeliveryRunList)
甚至不清楚控制器中的代码在哪里:
CustomerPart custPart = _custService.Get(custId);
if (DeliveryRunList.HasValue)
{
custPart.DeliveryRun_Id = DeliveryRunList.Value;
}
_custService.Update(custPart);
一种常见的方法是使用两个同名的方法进行编辑,一个用于HttpGet,一个用于HttpPost,并在razor视图中使用@Html.BeginForm()
进行更新,而不是更新controller中的信息
例如:
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">
}
@使用(Html.BeginForm()){
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
@*用于模型的属性*@
@Html.LabelFor。。。
@Html.EditorFor。。。
@Html.ValidationMessageFor。。。
}
听起来您只需要在加载页面时填充它。获取customers town并设置下拉列表的选定索引=town值(假设您绑定了town id/town文本中的数据和文本字段),您是否调试了该语句以确保更新实际发生。。。我认为,如果您将现有值绑定到下拉列表,那么它将始终具有一个值…请发布定义DeliveryRunList的代码。感谢您花时间给我一个详细的答复…这确实解决了最初的问题…现在加载页面时将显示正确的交付运行。然而,新的问题是:如果用户试图“保存”页面,deliveryRunRecord.Id将返回NULL并抛出未设置为对象实例的对象引用……知道吗?我不知道deliveryRunRecord是什么。你最好打开一个新的问题,提供更多关于你的计划的细节。看见
@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">
}