Hi I am new to MVC and even asp..
I want to create a form in MVC. With the help of some examples I am able to create TextBoxes, but I now I don't understand how to create Select List./
I tried searching many examples for implementing Select List in MVC, but I am not able to understand.
I have a Form which is half coded in HTML and half in MVC.
Here is my Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MedAvail.Applications.MedProvision.Web.Models
{
public class AddressViewModel
{
public string Street1 { get; set; }
public string Street2 { get; set; }
public string City { get; set; }
public string Province { get; set; }
public string Country { get; set; }
public string PostalCode { get; set; }
public string PhoneNumber { get; set; }
}
}
<form id="locationInfo">
<h1>Location Information</h1>
<table width="80%" id="locInfo">
<colgroup>
<col width="20%" />
<col />
</colgroup>
<tr>
<th>@Html.Label("Country")</th>
<td>
<select required="">
<option>Select Country</option>
<option>Canada</option>
<option>United States</option>
</select>
<span class="required">*</span>
</td>
</tr>
<tr>
<th>@Html.LabelFor(x=>x.State)</th>
<td>
<select required="">
<option>Select State</option>
<option>State 1</option>
<option>State 2</option>
<option>State 3</option>
...............
</select><span class="required">*</span></td>
</tr>
<tr>
<th>@Html.LabelFor(x=>x.PostalCode)</th>
<td>@Html.TextBoxFor(x=>x.PostalCode)<span class="required">*</span></td>
</tr>
<tr>
<th>@Html.LabelFor(x=>x.City)</th>
<td>@Html.TextBoxFor(x=>x.City)<span class="required">*</span></td>
</tr>
<tr>
<th>@Html.LabelFor(x=>x.StreetAddress1)</th>
<td>@Html.TextBoxFor(x=>x.StreetAddress1)<span class="required">*</span></td>
</tr>
<tr>
<th>@Html.LabelFor(x=>x.StreetAddress2)</th>
<td>@Html.TextBoxFor(x=>x.StreetAddress2)</td>
</tr>
<tr>
<th>@Html.LabelFor(x=>x.PhoneNumber)</th>
<td>@Html.TextBoxFor(x=>x.PhoneNumber)</td>
</tr>
</table>
<div role="button" class="marginTop50 marginBottom">
<input type="button" id="step3Back" value="Back" class="active" />
<input type="button" id="step3confirmNext" value="Next" class="active marginLeft50" />
</div>
</form>
Please guide me on how to create the Select List for this kind of form.
Thank You All! I am able to to load Select List as per MVC now My Working Code is below:
HTML+MVC Code in View:-
Created a Controller under "UTIL" folder: Code:-
And its working COOL now :-)
Thank you!!
How we do it is put this method into a class and then call the class from the view
I too liked Jordan's answer and implemented it myself. I only needed to abbreviations so in case someone else needs the same:
Thank you for this
Here's what I did:
1.Created an Extensions.cs file in a Utils folder.
2.In my model, where state will be abbreviated (e.g. "AL", "NY", etc.):
2.In my view I referenced it:
Designing You Model:
Prepare and bind List to Model in Controller :
In You View :
So many ways to do this... for #NetCore use Lib..
Model...
Controller...
pickState.cshtml...