Trouble with ASP.NET MVC 3 DropDownListFor

本文关键字:DropDownListFor MVC NET with ASP Trouble | 更新日期: 2023-09-27 17:56:02

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

我正在尝试创建一个下拉列表,该列表将在"创建任务"页面中显示应用程序名称,但将应用程序 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;
        }
    }
}

任务控制器:

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);
        }
    }
}        

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>

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

Trouble with ASP.NET MVC 3 DropDownListFor

我终于开始工作了。我需要创建一个新的应用程序对象:

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