我已经制作了一个Profile Model
存储某些属性值的。比如firstName、lastName……等等……其中state
就是其中之一。
现在,当我TextBox
用DropDownList
for State
property 替换时出现了问题。这就是我Edit
在ProfileController
.
如果有任何现有值,则打开应用程序时将填充。第一个问题,如何从下拉列表中获取选定的值,所以我可以Profile
像在这个方法中那样将它传递给属性。
public ActionResult Edit(string username)
{
ViewBag.StateID = new SelectList(db.States, "StateID", "StateName");
ProfileBase _userProfile = ProfileBase.Create(username);
ProfileModel _profile = new ProfileModel();
System.Web.HttpContext.Current.Session["_userName"] = username;
if (_userProfile.LastUpdatedDate > DateTime.MinValue)
{
_profile.FirstName = Convert.ToString(_userProfile.GetPropertyValue("FirstName"));
_profile.LastName = Convert.ToString(_userProfile.GetPropertyValue("LastName"));
_profile.Address = Convert.ToString(_userProfile.GetPropertyValue("Address"));
_profile.City = Convert.ToString(_userProfile.GetPropertyValue("City"));
_profile.State = Convert.ToString(_userProfile.GetPropertyValue("State"));
_profile.Zip = Convert.ToString(_userProfile.GetPropertyValue("Zip"));
}
return View(_profile);
}
State
当传入一个字符串TextBox
,然后使用Edit
post 方法保存时,这工作正常。
[HttpPost]
public ActionResult Edit(ProfileModel model)
{
if (ModelState.IsValid)
{
ProfileBase profile = ProfileBase.Create(System.Web.HttpContext.Current.Session["_userName"].ToString(), true);
if (profile != null)
{
profile.SetPropertyValue("FirstName", model.FirstName);
profile.SetPropertyValue("LastName", model.LastName);
profile.SetPropertyValue("Address", model.Address);
profile.SetPropertyValue("City", model.City);
profile.SetPropertyValue("State", model.State);
profile.SetPropertyValue("Zip", model.Zip);
profile.Save();
}
else
{
ModelState.AddModelError("", "Error writing to Profile");
}
}
return RedirectToAction("Index");
}
这就是我为State
.
模型:
public class State
{
public int StateID { get; set; }
public string StateName { get; set; }
public IEnumerable<RegisterModel> RegModel { get; set; }
public IEnumerable<ProfileModel> Profiles { get; set; }
}
控制器:
ViewBag.StateID = new SelectList(db.States, "StateID", "StateName");
看法:
@Html.DropDownList("StateID", (SelectList)ViewBag.StateID, new { @class = "dropdown" })
我已经尝试了几件事。到目前为止没有运气。我错过了什么?!