I'm new in ASP.net MVC and Razor.
I want to save user birthdate using Drop down list , But I don't know how access selected value in controller .
I used TextBoxFor
before that it's not appropriate for get date from user .
I read a lot of similar question but I didn't understand .
a part of my Register.cshtml :
<div class="form-group">
<label class="label">BirthDate</label>
<div class="col-md-10">
@Html.TextBoxFor(model => model.UserBirthDate)
@Html.ValidationMessageFor(model => model.UserBirthDate)
<select name="Year">
<option value="2000">2000</option>
<option value="2001">2001</option>
<option value="2002">2002</option>
<option value="2003">2003</option>
<option value="2004">2004</option>
<option value="2005">2005</option>
<option value="2006">2006</option>
</select>
<select name="Month">
<option value="7">July</option>
<option value="9">September</option>
<option value="12">December</option>
</select>
<select name="Day">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
</div>
</div>
a part of my Controller :
[HttpPost]
public ActionResult Register(User user)
{
UserRepositories bluser = new UserRepositories();
if (ModelState.IsValid)
{
if(user.UserBirthDate != null)
{
/////
}
if (bluser.Add(user))
{
return MessageBox.Show("Succsess", MessageType.Success);
}
else
{
return MessageBox.Show(" UNSuccsess", MessageType.Error);
}
}
else
{
return MessageBox.Show(ModelState.GetErrors(), MessageType.Warning);
}
}
user model :
public partial class User
{
public int UserID { get; set; }
public string UserEmail { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
public string UserPassWord { get; set; }
public string UserCellPhone { get; set; }
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
public Nullable<byte> UserStatus { get; set; }
public Nullable<System.DateTime> UserBirthDate { get; set; }
public string UserGender { get; set; }
}
usermetaData.cs
internal class UserMetaData
{
[ScaffoldColumn(false)]
[Bindable(false)]
public int UserID { get; set; }
public string UserEmail { get; set; }
public string UserFirstName { get; set; }
public string UserLastName { get; set; }
[DataType(DataType.Password)]
public string UserPassWord { get; set; }
public string UserCellPhone { get; set; }
public string UserTell { get; set; }
public string UserImage { get; set; }
public string UserAddress { get; set; }
public Nullable<byte> UserStatus { get; set; }
public Nullable<System.DateTime> UserBirthDate { get; set; }
public string UserGender { get; set; }
}
}
namespace NP1.Models
{
[MetadataType(typeof(NP1.Models.MetaData.UserMetaData))]
public partial class User
{
public string UserConfirmPassWord { get; set; }
public string Year { get; set; }
public string Month { get; set; }
public string Day { get; set; }
}
Best Answer
As David suggested in the comments, You should consider using a java script date picker library(Ex: JQuery UI,Bootstrap etc..) to set the date in UI which most of the users are familiar with. Dropdowns for dates seems to be a little old ! :(
If you still want to use dropdown's as you have in your question, you should create a view model for your view and add 3 properties for all the selected option values.
And in your GET action, you will send an object of this to your view,
And your view which is strongly typed to this view model
As long as your select element's name property value matches with the property names we have in our view model, MVC model binding will map the form data to an object of our view model when the form is submitted.
And if you ever want to set the selected option for the dropdown in an edit action method, take a look at this answer.