mechanicum
mechanicum

Reputation: 709

Html.DropDownListFor returns null

There are similar questions and I have tried most of them. Instead of continuing to destroy the remaining code while conjuring demons from the similar past questions, I have decided to ask for help.

When I arrive at the AddMembership action I get a null value instead of the selected item. Below are the details.

View;

@using (Html.BeginForm("AddMembership", "WorkSpace", FormMethod.Post, new { data_ajax = "true", id = "frmAddMembership" }))
{
 <div id="newMembershipDiv">

     @Html.DropDownListFor(m => m.selectedID, new SelectList(Model.allInfo, "Value","Text",1), "Select!")
     <input type="submit" value="Add" name="Command" /> 
 </div> 
}

Controller (I just want to see the selectedID or anything appear here.);

public ActionResult AddMembership(SelectListItem selectedID)
{
  return View();
}

Model;

public class SomeModel
{ 
    public SelectList allInfo { get; set; } 
    public SelectListItem selectedID { get; set; } 
} 

The Monstrosity which initializes the allInfo SelectList

model.allInfo = new SelectList(synHelper.getall().ToArray<Person>().Select(r => new SelectListItem {Text=r.Name, Value=r.prID.ToString() }));

synHelper.getAll() returns a List of the below class;

public class Person
{    
    public Guid prID { get; set; }
    public string Name { get; set; }
}

Upvotes: 0

Views: 530

Answers (1)

Andrei
Andrei

Reputation: 56688

The only thing that is posted to your action is a selectedID, which is a simple string. If you wander, in the request it looks as simple as:

selectedID=1234

Therefore it should be awaited for as a simple string. Adjust your action parameter:

public ActionResult AddMembership(string selectedID)

Upvotes: 3

Related Questions