Reputation: 115
I'm trying to submit a form using Html helper like this:
@using (Html.BeginForm("Create", "Certificate", FormMethod.Post, new { enctype = "multipart/form-data", id = "complianceCertificate" }))
Onload of page, I'm binding the dropdown using HTML helper like this
@Html.DropDownListFor(model => model._DetailsOfOwner.Id, Model._DetailsOfOwner.Practices, "Select licensee name...", new { @class = "form-control tx-13 pd-2 has-check-valid", @name = "LicenseeId" })
@Html.ValidationMessageFor(model => model._DetailsOfOwner.Practices, "", new { @class = "text-danger has-error-valid" })
The problem is when submitting the form. It passes null
value in model of SelectList
.
Here is my class where select list located
public class _DetailsOfOwnerViewModel : BaseAddress
{
public int Id { get; set; }
public SelectList Practices { get; set; }
public string LicenseeName { get; set; }
public string RMLNo { get; set; }
public string ContactPerson { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
}
Thank you.
Upvotes: 0
Views: 1134
Reputation: 59
Here, You are binding a dropdown from the model object and may b that's why you take SelectList for practices property. But when you receive form data using the post method at that time you have only a single value selected in the dropdown so you need int or string for that. So there is two way to fix it:
Bind dropdown by using ViewBag, in which you will assign your list and replace
public SelectList Practices {get;set;}
to
public string Practices {get;set;}
and bind dropdown by using model._DetailsOfOwner.Practices property instead of model._DetailsOfOwner.Id.
OR another way is
You are binding the model._DetailsOfOwner.Id in the dropdown so simply you can get selected value from that property.
Upvotes: 0
Reputation: 280
You cant return complete SelectList you need to select a single value from dropdown that will be returned to server. You should change your model.
Switch this
public SelectList Practices { get; set; }
to this
public string Practices { get; set; }
Upvotes: 1
Reputation: 672
A dropdown list when an item has been chosen maps to a single value rather than a list of values so the model isn't being bound properly on submission. Try changing the type of Practicies to an int or a string.
Upvotes: 0