ASP MVC + Html.DropDownList()使用ModelView模式的问题

时间:2009-06-26 08:02:07

标签: asp.net asp.net-mvc html-helper

最近我发布了一个关于html帮助器下拉列表的问题,并使其正常工作(here)。但是现在我已经决定切换到ModelView Patterns会更聪明,所以我可以在我的视图中访问强类型方法等。我做的是我通过以下方式对我的其他主题中的代码进行了一些调整:

VacatureFormViewModel:

public class VacaturesFormViewModel
{
    public Vacatures Vacature { get; private set; }
    public SelectList EducationLevels { get; private set; }
    public SelectList Branches { get; private set; }
    public SelectList CareerLevels { get; private set; }

    Repository repository;

    // Constructor
    public VacaturesFormViewModel(Vacatures vacature)
    {
        this.Vacature = vacature;
        this.repository = new Repository();
        this.EducationLevels = new SelectList(repository.GetAllEducationLevels(),"ID","Name",vacature.EducationLevels);
        this.Branches = new SelectList(repository.GetAllBranches(),"ID","Name",vacature.Branches);
        this.CareerLevels = new SelectList(repository.GetAllCareerLevels(), "ID", "Name", vacature.CareerLevels);

    }
}

BanenController:

//
    // GET: /Banen/Create

    public ActionResult Create()
    {
        Vacatures vacature = new Vacatures();
        return View(new VacaturesFormViewModel(vacature));
    }

    //
    // POST: /Banen/Create

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Vacatures vacatureToAdd)
    {
        if (ModelState.IsValid)
        {
            try
            {
                // TODO: Add insert logic here
                repository.AddToVacatures(vacatureToAdd);
                repository.SaveChanges();

                // Return to listing page if succesful
                return RedirectToAction("Index");
            }
            catch (Exception e)
            {
                return View();
            }
        }
    }

我的Create.aspx视图(部分内容):

<% using (Html.BeginForm()) {%>

    <fieldset>
        <legend>Fields</legend>
        <p>
            <label for="Title">Title:</label>
            <%= Html.TextBox("Title", Model.Vacature.Title) %>
            <%= Html.ValidationMessage("Title", "*") %>
        </p>
        <p>
            <label for="Content">Content:</label>
            <%= Html.TextArea("Content", Model.Vacature.Content) %>
            <%= Html.ValidationMessage("Content", "*") %>
        </p>
        <p>
            <label for="EducationLevels">EducationLevels:</label>
            <%= Html.DropDownList("EducationLevels", Model.EducationLevels)%>
            <%= Html.ValidationMessage("EducationLevels", "*") %>
        </p>
        <p>
            <label for="CareerLevels">CareerLevels:</label>
            <%= Html.DropDownList("CareerLevels", Model.CareerLevels)%>
            <%= Html.ValidationMessage("CareerLevels", "*")%>
        </p>
        <p>
            <label for="Branches">Branches:</label>
            <%= Html.DropDownList("Branches", Model.Branches)%>
            <%= Html.ValidationMessage("Branches", "*")%>
        </p>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>

为了指导我使用了ScottGu的NerdDinner教程,我在这里阅读了各种主题。

我的问题是,是否有可能让MVC ASP自动设置我的职业级别,educationlevel和branche(下拉列表),因为它当前正在返回一个不是我想要的ID字符串。当我将SelectList的创建更改为:

this.CareerLevels = new SelectList(repository.GetAllCareerLevels(), vacature.CareerLevels);

所以没有“ID”和“Name”它也不会保存(我猜它仍然作为post方法中的字符串返回,而不是对象本身)并且在此旁边,它在视图中列为:vacature.EducationLevels等所以不是名字,而是列出了对象本身。

最终问题 所以,简而言之,我的问题是,是否有可能使用这种方法来设置我的分支,教育水平和职业水平。那么不是自动的?

在这种情况下,我仍然需要使用以下内容:

[AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(FormCollection form)
    {
        Vacatures vacatureToAdd = new Vacatures();

        // Retrieve the education level by its ID
        if (!form["EducationLevels"].Equals(""))
        {
            Guid educationID = new Guid(form["EducationLevels"]);
            vacatureToAdd.EducationLevels = repository.GetEducationLevelByID(educationID);
        }

在我的控制器中?或者还有其他更顺畅的选择。

3 个答案:

答案 0 :(得分:2)

编辑使用Guid:

使用下拉列表,我使用稍微不同的方法。有可能让你的viewmodel工作,但对我来说这样更容易:

public class VacaturesFormViewModel
{

    public IEnumerable<SelectListItem>EducationLevels{ get; set; }
    public Guid EducationLevelID{ get; set; }

}

EducationLevelID将具有您选择的下拉列表ID。 这是观点:

<%= Html.DropDownList("EducationLevelID", Model.EducationLevels)%>

控制器

  IEnumerable<SelectListItem> educationLevelList =
                            from level in GetLevelList()
                            select new SelectListItem
                            {
                                Text = level .Name,
                                Value = level.Uid.ToString()
                            };
  model.EducationLevels = educationLevelList ;

答案 1 :(得分:1)

我不确定,但我认为你应该创建模型粘合剂。 (David Hayden写了一个简单的模型绑定器)

答案 2 :(得分:1)

您可以自动绑定 educationID 参数:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Guid? educationID, FormCollection form)
{
    Vacatures vacatureToAdd = new Vacatures();

    if (educationID != null)
    {
        vacatureToAdd.EducationLevels = 
            repository.GetEducationLevelByID(educationID.Value);
    }