i'm new in MVC and C# and for a project im trying to let the user to multiselect cities from a list and store it to the DB later. Ive read couple of a few posts regarding this but couldnt figure out how to fix mine. I can store all the other items of my form:
@using ( Html.BeginForm( "AddProjectInfo", "Insert", FormMethod.Post, new {
enctype = "multipart/form-data"
@id = "insertform", @class = "form-horizontal col-md-4 col-md-offset-4"
} ) )
but for city it only stores the first selected item not all. can you please help me to fix this?
Here is my view:
<div class="form-group">
<label class="col-sm-3 col-form-label text-left">City * </label>
<div class="col-sm-9">
@Html.DropDownList(
"City",
new SelectListItem[] {
new SelectListItem {
Text = "Cambridge", Value = "Ca"
},
new SelectListItem {
Text = "Oxford", Value = "Ox"
},
new SelectListItem {
Text = "Sheffield", Value = "Sh"
}
},
"--Select Cities--",
new {
@class = "form-control",
required = true,
multiple = "multiple"
}
)
</div>
</div>
Here is my controller:
[HttpPost]
public ActionResult Insert( FormCollection frm ) {
Models.ProjectInfo p = new Models.ProjectInfo();
String[] Cities= frm.GetValues( "City" );
p.ContractId = frm[ "ContractId" ];
p.Year = Convert.ToDecimal( frm[ "Year" ] );
p.City = Cities.ToString();
p.Location = frm[ "Location" ];
// Insert into the Database
AddProjectInfo( p );
ViewBag.ResponseMessage = "Submitted !";
}
I know how to do that using JavaScript but dont know how C# can handle that. Thanks you!