Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6664591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:37:40+00:00 2026-05-26T02:37:40+00:00

I’ve been researching this issue, but haven’t been able to piece together an answer

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:37:41+00:00Added an answer on May 26, 2026 at 2:37 am

    I finally got this working. I needed to create a new Application object:

    @Html.DropDownListFor(model => model.ApplicationID, new TaskCapture.Models.Application().GetApplications()) 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.