繁体   English   中英

ASP.NET MVC 3 DropDownListFor的问题

[英]Trouble with ASP.NET MVC 3 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>

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM