我很困惑如何让下拉列表显示所选的值。
型号:
public class SampleModel
{
public string State { get; set; }
}
控制器:
public ActionResult EditInformation()
{
ViewBag.State = new SelectList(db.States, "StateName", "StateName");
string userEmail = User.Identity.GetUserName();
Sample model = new SampleModel();
model.State = "Melbourne";
return View(model);
}
查看:
@Html.DropdownListFor(m => m.State, ViewBag.State as IEnumerable<SelectListItem>, "-- Select State --")
列表显示状态很好,但它没有自动选择我指定的值(&#34;墨尔本&#34;)。我已经尝试使用selectlist构造函数来分配selectedValue,但是在做了大量研究后,有人写道如果使用Html.DropdownListFor()
则使用selectlist构造函数是多余的,因为你将使用分配给它的值。模型。
编辑:
这是我的db.State模型:
public class State
{
[Key]
public int StateId { get; set; }
public string StateName { get; set; }
}
再次澄清一下,我想使用StateName作为值和selectlistitem的文本。
编辑: 我的完整行动方法:
public ActionResult EditInformation()
{
//var states = ndb.States.Select(s => new SelectListItem { Text = s.StateName, Value = s.StateName , Selected = s.StateName == "Jawa Timur" }).ToList();
ViewBag.State = new SelectList(ndb.States, "StateName", "StateName");
ViewBag.City = new SelectList(ndb.Cities, "CityName", "CityName");
string[] countries = { "Indonesia" };
ViewBag.Country = new SelectList(countries);
string userEmail = User.Identity.GetUserName();
try
{
UserInformation userInfo = ndb.UserInformations.Single(m => m.Email == userEmail);
UserAccountViewModel model = new UserAccountViewModel();
model.Address = userInfo.Address;
model.Email = userEmail;
model.FirstName = userInfo.FirstName;
model.LastName = userInfo.LastName;
model.Phone = userInfo.Phone;
model.PostalCode = userInfo.PostalCode;
Debug.Print(userInfo.State);
model.State = userInfo.State;
model.City = userInfo.City;
model.Country = userInfo.Country;
return View(model);
}catch { }
return View();
}
答案 0 :(得分:1)
iron:router@1.0.12
iron:middleware-stack@1.0.11
underscore@1.0.6
在EditInformation View中,您需要有一个actionlink链接到用户的ID,以便您提取正确的信息,所以:
EditInformation View:
public ActionResult EditInformation(int? id /*this will be passed from your route values in your view*/)
{
State myState = db.States.Find(id)
ViewBag.State = new SelectList(ndb.States, "StateId", "StateName", myState.StateId);
}//I only added this because this is what the question pertains to.
答案 1 :(得分:0)
试试这个:
public class SampleModel
{
public int Id { get; set; }
public string State { get; set; }
}
控制器:
public ActionResult EditInformation()
{
//Select default value like this (of course if your db.States have an Id):
ViewBag.State = new SelectList(db.States, "Id", "StateName", 1 /*Default Value Id or Text*/);
. . .
return View();
}
SelectList(IEnumerable, String, String, Object)
- 使用列表的指定项目,数据值字段,数据文本字段,和选定值初始化SelectList类的新实例。
查看:
@Html.DropdownList("State", null, "-- Select State --")
或者像你一样:
@Html.DropdownListFor(m => m.State, ViewBag.State as IEnumerable<SelectListItem>, "-- Select State --")
更新:
您可以使用jQuery获取Selected文本,如下所示:
添加@Html.HiddenFor(x => x.State)
@Html.DropdownListFor(m => m.State, ViewBag.State as IEnumerable<SelectListItem>, "-- Select State --", new { id = "stateId" })
@Html.HiddenFor(x => x.State)
JS:
$(function () {
$("form").submit(function(){
var selectedText= $("#stateId :selected").text();
$("#State").val(selTypeText);
});
});
发表:
[HttpPost]
public void UploadDocument(State model)
{
if(ModelState.IsValid)
{
string state = model.State;
}
}
答案 2 :(得分:0)
好的,所以经过一段时间的研究,问题在于命名惯例。显然,您无法将ViewBag.State
用于Html.DropdownListFor(m => m.State)
,这会导致Html.DropdownListFor(m => m.State)
无法正确读取数据。