Reputation: 113
I have a question about radio buttons and checks.
So in first, in my views part I have a textbox with a login value. Between this textbox and the submit button, I have 5 radio buttons. Each of these rb referenced a property in my ViewModel.
So here is the code. My ViewModel with the properties:
public class DeleteUAInfosViewModel
{
[Display(Name = "UserAccountDeleteInfos", ResourceType = typeof(UserAccountResources))]
public string UserLeave
{
get { return UserAccountResources.UserLeave; }
}
[Display(Name = "UserAccountDeleteInfos", ResourceType = typeof(UserAccountResources))]
public string UserTransfer
{
get { return UserAccountResources.UserTransfer; }
}
[Display(Name = "UserAccountDeleteInfos", ResourceType = typeof(UserAccountResources))]
public string UserDetachment
{
get { return UserAccountResources.UserDetachment; }
}
[Display(Name = "UserAccountDeleteInfos", ResourceType = typeof(UserAccountResources))]
public string UserRetirement
{
get { return UserAccountResources.UserRetirement; }
}
[Display(Name = "UserAccountDeleteInfos", ResourceType = typeof(UserAccountResources))]
public string UserStatus
{
get { return UserAccountResources.UserStatus; }
}
}
My IndexViewModel where I link the DeleteUAInfosViewModel:
public class IndexViewModel
{
[Display(Name = "DeleteUAInfos", ResourceType = typeof(UserAccountResources))]
public DeleteUAInfosViewModel DeleteUAInfosGroup { get; set; }
public IndexViewModel()
{
DeleteUAInfosGroup = new DeleteUAInfosViewModel();
}
...
}
My partial View with the declaration of rb:
@model MyPath.UserAccount.DeleteUAInfosViewModel
<div id="UserDeleteInfosField">
<p>
@Html.RadioButton("RbDeleteGroup", Model.UserLeave)
@UserAccountResources.UserLeave
@Html.HiddenFor(x => x.UserLeave)
</p>
<br />
<p>
@Html.RadioButton("RbDeleteGroup", Model.UserTransfer)
@UserAccountResources.UserTransfer
@Html.HiddenFor(x => x.UserTransfer)
</p>
<br />
<p>
@Html.RadioButton("RbDeleteGroup", Model.UserDetachment)
@UserAccountResources.UserDetachment
@Html.HiddenFor(x => x.UserDetachment)
</p>
<br />
<p>
@Html.RadioButton("RbDeleteGroup", Model.UserRetirement)
@UserAccountResources.UserRetirement
@Html.HiddenFor(x => x.UserRetirement)
</p>
<br />
<p>
@Html.RadioButton("RbDeleteGroup", Model.UserStatus)
@UserAccountResources.UserStatus
@Html.HiddenFor(x => x.UserStatus)
</p>
</div>
I call this partial view in another view with this line:
@Html.Partial("_DeleteUAInfos", Model.DeleteUAInfosGroup)
Currently I performed a check if the login is null or empty.
But now I need to include the check of the radio buttons. Example: I want to verify if the login is correct AND at least one rb is checked (and retrieved the value).
In another words: I want to integrate the check of the radio buttons with the check of the login value (in my case the login part is already done).
So can you help me with that ?
Thanks in advance !
Ars_n
Upvotes: 1
Views: 1596
Reputation: 113
Finally I totally changed the way to do, using Enumerations. So, I followed these steps:
1) Declaration of a public enum in the ViewModel (which contains "UserLeave", "UserTransfer", etc..).
2) Reference the enum (in the ViewModel) with a property: public MyEnum myEnum{ get; set; }
3) Call this enum in the View: @Html.RadioButtonFor(m => m.myEnum, MyEnum.UserLeave);
repeat this action for each radio button.
4) Now, I can perform checks (Radio button is checked or not) directly in my ViewModel..
So I hope it'll help somebody who had the same problem..
Regards,
Ars_n
Upvotes: 0
Reputation: 3416
Well all you do is create essentially one really long boolean expression, so in the function to validate your login add
if(!radio1&&!radio2...&&!radion)
{
ModelState.AddModelError("HTMLModelComponentYouWantToValidate", "ErrorMessage");
}
and then before continuing add
if(ModelState.IsValid){...}
else{ return View();}
Upvotes: 2