ASP.NET MVC 3 DropDownListFor的问题

时间:2011-05-11 21:08:11

标签: c# asp.net-mvc-3 html.dropdownlistfor

我一直在研究这个问题,但是无法拼凑出一个适合我情况的答案。我感谢任何帮助。

我正在尝试创建一个下拉列表,该列表将在“创建任务”页面中显示应用程序名称,但会将应用程序ID发回数据库。目前,当我使用DropDownList时,我能够正确地在下拉列表中显示应用程序名称。但是,回发不起作用,我想使用DropDownListFor。此外,我正在使用Dictionary(int,String>,但我更喜欢使用IEnumerable。我一直在试图让DropDownListFor工作,但到目前为止没有运气。有谁知道我改变了什么需要制作?

'任务'模型:

using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace TaskCapture.Models
{
    public class Task
    {
        public int TaskID { get; set; }
        public string TaskDescription { get; set; }
        public int ApplicationID { get; set; }
        public virtual Application Application { get; set; }
    }
} 

'应用'模型:

using System;
using System.Collections.Generic;

namespace TaskCapture.Models
{
    public class Application
    {
        public int ApplicationID { get; set; }
        public string ApplicationName { get; set; }

        public override string ToString()
        {
            return ApplicationName;
        }
    }
}

TaskController:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TaskCapture.Models;
using TaskCapture.DAL;

namespace TaskCapture.Controllers
{ 
    public class TaskController : Controller
    {
        private SupportContext db = new SupportContext();

        //
        // GET: /Task/Create

        // get application names for listing in drop down
        public Dictionary<int, String> GetApplications()
        {

            Dictionary<int, String> _applications = new Dictionary<int, String>();

            var applicationsQuery =
                from o in db.Applications
                orderby o.ApplicationName
                select o;

            foreach (var c in applicationsQuery)
            {
                _applications.Add(c.ApplicationID, c.ApplicationName);
            }

            return _applications;
        }

        public ActionResult Create()
        {
            Dictionary<int, String> Applications = this.GetApplications();

            ViewData["ApplicationList"] = new SelectList(Applications, "Key", "Value");

            return View();
        } 

        //
        // POST: /Task/Create

        [HttpPost]
        public ActionResult Create(Task task)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    db.Tasks.Add(task);
                    db.SaveChanges();
                    return RedirectToAction("Index");  
                }
            }
            catch (DataException)
            {
                //Log the error (add a variable name after DataException)
                ModelState.AddModelError("", "Unable to save changes. Try again....");
            }

            return View(task);
        }
    }
}        

任务Create.cshmtl:

@model TaskCapture.Models.Task        

@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Task</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Application.ApplicationName)
        </div>
        <div class="editor-field">
            @Html.DropDownList("ApplicationList", (SelectList) ViewData["ApplicationList"])
            @Html.ValidationMessageFor(model => model.Application.ApplicationName)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

<div>
    @Html.ActionLink("Back to List", "Index")
</div>

感谢任何帮助。如果我需要发布更多信息,请告诉我。感谢。

1 个答案:

答案 0 :(得分:1)

我终于有了这个工作。我需要创建一个新的Application对象:

@Html.DropDownListFor(model => model.ApplicationID, new TaskCapture.Models.Application().GetApplications())