0

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

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

任务创建.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>

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

4

1 回答 1

1

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

@Html.DropDownListFor(model => model.ApplicationID, new TaskCapture.Models.Application().GetApplications()) 
于 2011-10-11T17:11:53.877 回答