【问题标题】:problems with ASP MVC + Html.DropDownList() using a ModelView Pattern使用 ModelView 模式的 ASP MVC + Html.DropDownList() 问题
【发布时间】:2009-06-26 08:02:07
【问题描述】:

最近我发布了一个关于 html helper 下拉列表的问题并让它工作 (here)。但是现在我决定切换到 ModelView 模式要聪明得多,这样我就可以在我的视图等中访问强类型方法。我所做的是我通过以下方式对我的另一个主题中的代码进行了一些调整:

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 自动设置我的职业级别、教育级别和分支(下拉列表),因为它当前返回的 ID 字符串不是我想要的。当我将 SelectList 的创建更改为:

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

因此,如果没有“ID”和“名称”,它也不会保存(我猜它仍然在 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);
        }

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

【问题讨论】:

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


    【解决方案1】:

    编辑为使用 Guid:

    对于下拉列表,我使用了稍微不同的方法。有可能让您的视图模型工作,但对我来说,这样更容易:

    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 ;
    

    【讨论】:

    • 问题是我的 EducationLevelID 不是 int(下拉列表支持?),而是一次不支持的 GUID。据我所知,您必须自己从下拉列表的返回中生成 GUID。这意味着我仍然需要执行之前的步骤。
    • 我检查了,Guid 没有问题。编辑后的代码使用 IEnumerable 但与 SelectList 应该没有区别
    • 那么,下拉列表如何使用“EducationLevelID”适当地显示所选项目?
    【解决方案2】:

    我不确定,但我认为您应该创建模型活页夹。 (David Hayden 写了一个简单的模型绑定器)

    【讨论】:

    • 这也是仅基于字符串的信息的示例。正如他使用的那样: HttpContext.Request.Form["Name"];在我的情况下,这正是出问题的地方,它没有给出我认为的对象,而是一个字符串。不过不确定。
    • 另一方面,这实际上可能是:很有希望。
    【解决方案3】:

    您可以自动绑定 educationID 参数:

    [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Create(Guid? educationID, FormCollection form)
    {
        Vacatures vacatureToAdd = new Vacatures();
    
        if (educationID != null)
        {
            vacatureToAdd.EducationLevels = 
                repository.GetEducationLevelByID(educationID.Value);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多