Click here to Skip to main content
   

ASP.NET

 
AnswerRe: login object in asp.net PinmvpSandeep Mewara25-Jan-13 6:03 
AnswerRe: login object in asp.net PinmvpRahul Rajat Singh25-Jan-13 20:17 
QuestionExperience with BootMetro? PinmemberMarco Alessandro Bertschi25-Jan-13 0:06 
AnswerRe: Experience with BootMetro? PinmemberAbhishek Pant25-Jan-13 11:28 
Questionsignature capture from signature pad Pinmemberpremaa_36@yahoo.co.in24-Jan-13 18:36 
AnswerRe: signature capture from signature pad PinmvpSandeep Mewara25-Jan-13 6:03 
AnswerRe: signature capture from signature pad PinmemberAbhishek Pant25-Jan-13 10:51 
QuestionObtaining the SelectedValue of a dropdownlist PinmemberCollin Jasnoch24-Jan-13 3:50 
I am using MVC 4 and I can not get the selected value. I have no idea what I am doing wrong. It seems this should be straight forward but I have pounded my brain on my keyboard for hours now.
 
I have an EntityController and on its construction it builds up a view model for the view. In other words:
 
private AppDBContext _db = new AppDBContext();
private Entities _viewModel = new Entities();
public EntityController()
{
   _viewModel.ActiveEntities = _db.Entities.ToList();
   var platformQry = _db.Platforms.AsEnumerable();
   _viewModel.Platforms = new List<Platform>(platformQry.Distinct());
}
 
Platform is just a simple 2 column item that has an ID and a name. i.e. :
public class Platform
{
    public int ID { get; set; }
 
    [Required]
    public string Name { get; set; }
}
 
The view model is quite simple. It is maintaining the available Platform items, has a value to track the ID of the selected item, and contains a collection of the POCOs (Entity). i.e.:
public class Entities
{
     public IEnumerable<Entity> ActiveEntities { get; set; }
 
     public IEnumerable<Platform> Platforms { get; set; }
 
     public int PlatformFilterId { get; set; }
 
}
 
Now my intention is to use this value (the ID) in a callback to the controller to filter down the ActiveEntities. So here is the Action method (it is just the Index of the controller).
 
public ActionResult Index()
{
    //Build the initial entity query to filter down
    _viewModel.ActiveEntities = _db.Entities.AsEnumerable();
 
    if (_viewModel.PlatformFilterId != 0)
    { 
        //Filter by the platform ID
        _viewModel.ActiveEntities = _viewModel.ActiveEntities.Where(e => e.Platform == _viewModel.PlatformFilterId);
    }
 
    //Ensure the query is run
    _viewModel.ActiveEntities = _viewModel.ActiveEntities.ToList();
    return View(_viewModel);
}
 
Seems simple enough but maybe this is already wrong.... And here is the view:
 
@model AppName.ViewModels.Entities
 
@{
    ViewBag.Title = "Entity Overview";
}
 
<h2>Index</h2>
 
<p>
    @Html.ActionLink("Create New", "Create")
 
    @using (Html.BeginForm("Index", "Entity", FormMethod.Get))
    {
 
        <p>            
            @Html.DropDownListFor(x => x.PlatformFilterId , new SelectList(Model.Platforms, "ID", "Name"), "-- Filter by Platform--")
            <input type="submit" value="Filter" />
        </p>
    }
</p>
<table>
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.ActiveEntities.First().Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.ActiveEntities.First().Name)
        </th>
        <th></th>
    </tr>
 
@foreach (var item in Model.ActiveEntities) {
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.Name)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.CreatedDate)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.ID }) |
            @Html.ActionLink("Details", "Details", new { id=item.ID }) |
            @Html.ActionLink("Delete", "Delete", new { id=item.ID })
        </td>
    </tr>
}
 
</table>
 

The outcome is that I get my dropdown populated correctly, but on the postback (i.e. the Index action method firing after I click "Filter") the PlatformFilterId is always 0 so of course I can never filter. What am I doing wrong?
 
Any help is appreciated.
Computers have been intelligent for a long time now. It just so happens that the program writers are about as effective as a room full of monkeys trying to crank out a copy of Hamlet.

QuestionHow to get the value of a dynamically created textBox in asp.net C#? Pinmembersusanna.floora24-Jan-13 3:46 
AnswerRe: How to get the value of a dynamically created textBox in asp.net C#? Pingroupchester_it2124-Jan-13 15:50 
GeneralRe: How to get the value of a dynamically created textBox in asp.net C#? Pinmembersusanna.floora24-Jan-13 18:54 
GeneralRe: How to get the value of a dynamically created textBox in asp.net C#? Pingroupchester_it2129-Jan-13 5:47 
GeneralRe: How to get the value of a dynamically created textBox in asp.net C#? Pinmembersusanna.floora3-Feb-13 21:30 
GeneralRe: How to get the value of a dynamically created textBox in asp.net C#? Pingroupchester_it216-Feb-13 7:22 
QuestionURL Rewriting PinmemberMaheshPandian24-Jan-13 1:37 
AnswerRe: URL Rewriting PinmemberShameel24-Jan-13 2:00 
GeneralRe: URL Rewriting PinmemberMaheshPandian24-Jan-13 3:28 
QuestionHow to create loocal SMTP server in windows Pinmembersani_alam24-Jan-13 0:59 
AnswerRe: How to create loocal SMTP server in windows PinmvpRichard MacCutchan24-Jan-13 2:54 
QuestionHow to extract data of a table. Pinmemberanubhaw.gupta23-Jan-13 21:06 
Questionhow to use ckeditor in asp.net? PinmemberDaxiii23-Jan-13 1:09 
AnswerRe: how to use ckeditor in asp.net? Pinmembermark merrens23-Jan-13 6:31 
AnswerRe: how to use ckeditor in asp.net? PinmvpSandeep Mewara23-Jan-13 9:20 
Questionhow resize this image before uploading PinmemberJassim Rahma22-Jan-13 9:44 
AnswerRe: how resize this image before uploading Pinmemberjkirkerx22-Jan-13 13:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web03 | 2.8.150123.1 | Last Updated 27 Jan 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid