预先选择DropDownList的值时出现问题。 Basicaly我有一个ViewModel类,它封装了我以后不想创建的类的isntance以及DropDownList可供选择的所有可能值。 这是代码
模型类:
public abstract class Entity
{
public virtual int Id { get; set; }
}
public class Project : Entity
{
public virtual string Name { get; set; }
public virtual string Comments { get; set; }
public virtual IList<Activity> Activities { get; set; }
}
public class Activity : Entity
{
public virtual string Comments { get; set; }
public virtual Project Project { get; set; }
}
视图模型:
public class ActivityViewModel
{
public Activity Instance { get; set; }
public IList<Project> AllProjects { get; set; }
}
查看:
@model Desk.Mvc3.Models.ActivityViewModel
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Activity</legend>
<p>
<input type="submit" value="Create" />
</p>
<div class="editor-label">
@Html.LabelFor(model => model.Instance.Comments)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Instance.Comments)
@Html.ValidationMessageFor(model => model.Instance.Comments)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.AllProjects)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Instance.Project,
new SelectList(Model.AllProjects, "Id", "Name", Model.Instance.Project.Id ))
@Html.ValidationMessageFor(model => model.AllProjects)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
......和控制者的行动:
public class ActivityController : PersistanceController
{
// ...
//
// GET: /Activity/Create/2
public ActionResult Create(int projectId)
{
var projects = GetRep<Project>().GetAll();
var activity = new Activity();
activity.Project = projects.SingleOrDefault(p => p.Id == projectId);
var item = new ActivityViewModel {
Instance = activity,
AllProjects = projects };
return View(item);
}
}
为什么在浏览器内部显示DropDownList时会使用默认的Project项而不是preselcted项(即已在Action方法中指定的项)填充?
答案 0 :(得分:3)
“DropDownListFor”中的第一个参数用于绑定,它会覆盖您选择的值。您正在使用“项目”对象,但您必须使用项目的“Id”。
@Html.DropDownListFor(model => model.Instance.Project.Id,
new SelectList(Model.AllProjects, "Id", "Name", Model.Instance.Project.Id ))
如果您不想要此行为,请使用“DropDownList”帮助程序。
更新(回复您的评论):
有一种方法可以完成以下工作。
@Html.DropDownListFor(model => model.Instance.Project, new SelectList(Model.AllProjects, "Id", "Name", Model.Instance.Project.Id ))
您需要覆盖“Project”类中的“ToString()”函数以返回“Id”。
public class Project : Entity
{
public virtual string Name { get; set; }
public virtual string Comments { get; set; }
public virtual IList<Activity> Activities { get; set; }
public override string ToString()
{
return Id.ToString();
}
}
另一个选择是使用“DropDownList”帮助器。
@Html.DropDownList("Instance.Project",
new SelectList(Model.AllProjects, "Id", "Name", Model.Instance.Project.Id ))
这将生成相同的html。
答案 1 :(得分:1)
我不知道你正在使用什么方法的数据库,我认为Model.Instance.Project在离开控制器部分后可能为null。尝试传递选定值throgh ViewData(或ViewBag):
public ActionResult Create(int projectId)
{
var projects = GetRep<Project>().GetAll();
var activity = new Activity();
var selProj = projects.SingleOrDefault(p => p.Id == projectId);
ViewData[SelectedProjectID] = selProj != null ? selProj.ID : -1;
var item = new ActivityViewModel {
Instance = activity,
AllProjects = projects };
return View(item);
}
查看:
@Html.DropDownListFor(model => model.Instance.Project,
new SelectList(Model.AllProjects, "Id", "Name", (int)ViewData[SelectedProjectID] ))
希望这会有所帮助。