質問

I've been researching this issue, but haven't been able to piece together an answer that works in my situation. I appreciate any help.

I'm trying to create a drop-down list that will show Application names in a Create Task page, but posts the Application ID back to the database. Currently, I'm able to get the Application names to show in the drop-down list correctly when I use DropDownList. However, the post back doesn't work, and I'd like to use DropDownListFor instead. Also, I'm using Dictionary(int, String>, but I'd prefer to use IEnumerable. I've been going around in circles trying to get the DropDownListFor to work, but no luck so far. Does anyone know what changes I need to make?

'Task' Model:

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

'Application' Model:

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

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>

Any help is appreciated. Please let me know, if I need to post more info. Thanks.

正しい解決策はありません

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top