0

Trying to unpack some inherited code and I am new to ASP.NET. My question:

What is available in the controller from a post action in a dropdownlist in C# (ASP.NET MVC5)?

Here is what I have in the view:

 @using (Html.BeginForm("SignUp", "Location", FormMethod.Post))
 ....
 @Html.DropDownListFor(
 model => model.Member.PillarId, new SelectList (Model.Pillars, "Id", "Title"))

Here is the MemberViewModel:

 public class MemberViewModel : IValidatableObject{
    public int? LocationId { get; set; }
    public int? PillarId { get; set; }  
     }

Here is the Member model:

public class Member
{
    public int Id { get; set; }        
    public string Name { get; set; }   

    public int? LocationId { get; set; }
    public int? PillarId { get; set; }
    public String PillarTitle { get; set; }

Here is the Member model constructor(s):

    public Member() { }
    public Member(MemberViewModel member)
    { 
        PillarId = member.PillarId;
        ///
    }

Here is the Controller

 public ActionResult SignUp(MemberViewModel member){///}

My form is correctly pulling the information from the DB to display, as well as posting correctly to the DB via the Controller. I do not want to change the visual options for the user to choose from (i.e. I think ListBox is out?).

Rather, I want to assign both Member.PillarId as well as the Member.Title based on their choice and have it available in the Controller for non-DB operations.

What is currently available in the SignUp method in the Controller? Can I call Model.Pillars.Title? Is it member.PillarId.Pillars.Id? If not, how can I assign it dynamically based on the user choice?

There are views and modelviews flying around in this code, and I am not sure what is available...

SO has a bunch of answers on the DropDownList, so here's a sampling of articles that are somewhat related to what I am getting at...
* This answer
* ListBox
* ListBoxFor: not MVC dynamic
* SelectList Constructor: Not MVC

Community
  • 1
  • 1
nrako
  • 2,952
  • 17
  • 30
  • Not clear what you are asking. Are you expecting to get the values of both `PillarId` and `PillarTitle` when you select an item from the dropdownlist? –  Feb 24 '15 at 21:50
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Feb 24 '15 at 22:02
  • I am getting PillarId. I need PillarTitle. Yes, I would like both. – nrako Feb 24 '15 at 22:20
  • A ` –  Feb 24 '15 at 22:31

1 Answers1

1

With a dropdownlist the only value that will come back is the value of the selected item when posting back.

If you want something else to come back you would need a hidden field on the page and bind a change event listener to the dropdown to set the title in the hidden field

public class MemberViewModel : IValidatableObject{
    public int? LocationId { get; set; }
    public int? PillarId { get; set; }  
    public string Title { get; set;}
}

@using (Html.BeginForm("SignUp", "Location", FormMethod.Post))
 ....
 @Html.DropDownListFor(
 model => model.Member.PillarId, new SelectList (Model.Pillars, "Id", "Title"))
 @Html.HiddenFor(model => model.Title);

javascript

 $('#idofdropdown').on('change', function()
  {
     var selectedTitle = $(this).find(":selected").text();
     $('#Title').val(selectedTitle);
  });

How to get selected title from a drop down list: Get selected text from a drop-down list (select box) using jQuery

Then in your controller your viewmodel will have the title text inside the Title string :)

Community
  • 1
  • 1
krilovich
  • 3,475
  • 1
  • 23
  • 33