How can I change input field to Datepicker based on radio button selection? - asp.net-mvc

I have an input field and it accepts strings inputted by the user. It looks for the string based on a radio button. Pic of Form
Basically I want that input field to turn into a date picker if the DOB or DOJ radio button is selected.
This is how my view looks like currently
<div class="row">
<div class="col-md-4">
<form asp-action="Find">
#Html.TextBoxFor(m => m.input)<br />
#Html.RadioButtonFor(m => m.radio, "ID")<aspan>ID</aspan><br />
#Html.RadioButtonFor(m => m.radio, "FirstName")<aspan>First Name</aspan><br />
#Html.RadioButtonFor(m => m.radio, "LastName")<aspan>Last Name</aspan><br />
#Html.RadioButtonFor(m => m.radio, "DOB")<aspan>DOB</aspan><br />
#Html.RadioButtonFor(m => m.radio, "DOJ")<aspan>DOJ</aspan><br />
#Html.RadioButtonFor(m => m.radio, "Phone")<aspan>Phone</aspan><br />
#Html.RadioButtonFor(m => m.radio, "Email")<aspan>Email</aspan><br />
<div class="form-group">
<input type="submit" value="Find" class="btn btn-default" />
</div>
</form>
</div>
</div>
<div>

Firstly, you should not use #Html.RadioButtonFor in that way. It will generate all the inputs with the same id. That would not be nice, as per specification ID-s should be unique.
Do it like this (give a different id for each input)
#Html.RadioButtonFor(model => model.radio, "FirstName", new { id = "FirstName" })
Secondly, you need to use JavaScript for what you want to achieve.
I am going with JQuery, but you can do it without it also. Use change method so you get the selected value for the radio button each time the user changes it. Then you can use .prop() to change the input type.
$('input[name=radio]').change(function() {
if (this.value=== 'DOB' || this.value=== 'DOJ') {
$('input[name=input]').prop('type', 'date');
} else {
$('input[name=input]').prop('type', 'text');
}
});

Related

MVC 5, Using Checkbox for same model property twice in a page but inside different forms

Model property which I intend to use inside two forms
public class TestModel
{
[Display(Name = "Terms Accepted")]
[Range(typeof(bool), "true", "true", ErrorMessage = "You need to accept terms and conditions to proceed!")]
public bool TermsAccepted { get; set; }
}
view Page
<form id="form1">
<div class="row">
<div class="col-md-3">
#Html.CheckBoxFor(m => m.TermsAccepted) I accept terms and conditions
#Html.ValidationMessageFor(m => m.TermsAccepted, new { #id = "chk1" })
</div>
<div class="col-md-3">
<input type="submit" id="button1" onclick="return isFormValid('form1');" value="Submit Form 1"/>
</div>
</div>
</form>
<form id="form2">
<div class="row">
<div class="col-md-3">
#Html.CheckBoxFor(m => m.TermsAccepted, new { #id = "chk2" }) I accept terms and conditions
#Html.ValidationMessageFor(m => m.TermsAccepted)
</div>
<div class="col-md-3">
<input type="submit" id="button2" onclick="return isFormValid('form2');" value="Submit Form 2"/>
</div>
</div>
</form>
When this UI is rendered on the page, checkbox inside the 1st form do contains attributes for RangeDataAnnotation while checkbox inside 2nd form doesn't have any attributes for data annotation. So this results into 2nd form doesn't throw any validation on submission.
Html of checkboxes which get rendered on UI
Inside form 1:
<input name="TermsAccepted" class="input-validation-error" id="chk1" aria-describedby="TermsAccepted-error" type="checkbox" value="true" data-val="true" data-val-range-min="True" data-val-range-max="True" data-val-range="You need to accept terms and conditions to proceed!">
Inside form 2:
<input name="TermsAccepted" id="chk2" type="checkbox" value="true">
Any suggestions to make this work in both forms?

Gridmvc filtering without query in url

I'm using GridMvc and I'm filtering data. I'd like to hide filter query in the url like http://www.mypage.com/Overview?Name=yyy
My form is defined as:
<form class="form-inline" method="POST" action="#Url.Action("Filter", Request.QueryString)">
<div class="form-group>
#Html.LabelFor(c => c.Name)
#Html.TextBoxFor(c => c.Name, new { placeholder = "Filter", #class = "form-control" })
<button type="submit" class="btn btn-default"><i class="glyphicon glyphicon-search"></i> Search</button>
</div>
</form>
And the action
[HttpPost]
public ActionResult Filter(FilterModel model)
But I always see the query. It's possible to hide query string?
You cound hide your query if you put all you data in hidden fields instead of Request.QueryString.
I mean if your Request.QueryString looks like param1=test1&param2=test2 you should render you view like this:
<form class="form-inline" method="POST" action="#Url.Action("Filter")">
<input type="hidden" name="param1" value="test1" />
<input type="hidden" name="param2" value="test2" />
<div class="form-group>
#Html.LabelFor(c => c.Name)
#Html.TextBoxFor(c => c.Name, new { placeholder = "Filter", #class = "form-control" })
<button type="submit" class="btn btn-default"><i class="glyphicon glyphicon-search"></i> Search</button>
</div>
</form>
MVC binding will bind all hidden values on POST according to name property of this hidden inputs.
You should just fill inputs value (replace test1 and test2 with your values from Request.QueryString)

How to bind input field to editfor model MVC

I have the following javascript and input fields within my model.
script type="text/javascript">
function sum() {
var txtFirstNumberValue = document.getElementById('money1').value;
var txtSecondNumberValue = document.getElementById('money2').value;
var txtThirdNumberValue = document.getElementById('money3').value;
var result = parseFloat(txtFirstNumberValue) + parseFloat(txtSecondNumberValue) + parseFloat(txtThirdNumberValue);
if (!isNaN(result)) {
document.getElementById('Total').value = result;
}
}
</script>
<div class="form-group">
#Html.LabelFor(model => model.Total, "Total", new { #class = "control-label col-md-5" })
<div class="col-md-offset-0">
<input type="text" id="Total" name="Total" />
#*#Html.EditorFor(model => model.Total)*#
#Html.ValidationMessageFor(model => model.Total)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.money1, "money1", new { #class = "control-label col-md-5" })
<div class="col-md-offset-0">
#*#Html.EditorFor(model => model.money1)*#
<input type="text" id="money1" onkeyup="sum();" name="money1" />
#Html.ValidationMessageFor(model => model.money1)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.money2, "money2", new { #class = "control-label col-md-5" })
<div class="col-md-offset-0">
#*#Html.EditorFor(model => model.money2)*#
<input type="text" id="money2" onkeyup="sum();" name="money2" />
#Html.ValidationMessageFor(model => model.money2)
</div>
</div>
<div class="form-group">
#Html.LabelFor(model => model.money3, "money3", new { #class = "control-label col-md-5" })
<div class="col-md-offset-0">
#*#Html.EditorFor(model => model.money3)*#
<input type="text" id="money3" onkeyup="sum();" name="money3" />
#Html.ValidationMessageFor(model => model.money3)
</div>
</div>
This all works fine. When I type a value into the 3 money fields the resulting value appears in the Total field. Within MVC if I click on the details view it shows all four values, however if I click on the edit view all four fields are blank. Question is how do I get the values to appear and remain in edit mode?
Well, for starters your model-bound input fields are commented out:
#*#Html.EditorFor(model => model.money1)*#
In order to get those to work you'd need to un-comment them.
You could just manually populate your markup with the model values, something like this:
<input type="text" id="money1" onkeyup="sum();" name="money1" value="#model.money1" />
That should pre-populate that input with the model's money1 value. Though you're assuming the responsibility of manually implementing anything else that the built-in HTML helpers would provide for you. Unless there's a compelling reason not to use Html.EditorFor() I imagine that would be the better option.

how to use radiobuttonlist in mvc4

I need to create a simple radio button list selection to choose from the following values:
Agree
Disagree
Not sure
How would we add it in the following razor view? Is it better to create an enum of these values in the QuestionModel and use foreach to bind with html helper.
Any example or ideas?
#model Survey.Models.Question
#using (Html.BeginForm("Index", "Home", new { QuestionId = Model.QuestionId }))
{
<h2>Survey</h2>
<fieldset>
<legend>Please Choose</legend>
<p>
Question ID:
#Model.QuestionId
</p>
<p>
Description:
#Model.Description
</p>
<input type="submit" value="Next" id="submitButton" />
</fieldset>
}
Using the ASP.NET Html Helpers, there are three various methods you can use for each of the different helpers supplied for radio buttons.
Method 1
A simple way to make a radio button list:
<div>
<label>
#Html.RadioButton("Answer", "Agree")
Agree
</label>
</div>
<div>
<label>
#Html.RadioButton("Answer", "Disagree")
Disagree
</label>
</div>
<div>
<label>
#Html.RadioButton("Answer", "Not Sure")
Not Sure
</label>
</div>
Method 2
The above code will post the checked value with a name of "Answer" to the server when you click Submit. If you would like to make this strongly typed, as you did with Model.QuestionId and Model.Description, you could add a Answer property to your model and do the following:
<div>
<label>
#Html.RadioButtonFor(m => m.Answer, "Agree")
Agree
</label>
</div>
<div>
<label>
#Html.RadioButtonFor(m => m.Answer, "Disagree")
Disagree
</label>
</div>
<div>
<label>
#Html.RadioButtonFor(m => m.Answer, "Not Sure")
Not Sure
</label>
</div>
Method 3
Finally, if you would like to get fancy, you can use an enum. Place the following in your controller:
public enum AnswerType {
Agree,
Disagree,
NotSure
}
Next, add the property to your model:
public AnswerType AnswerType { get; set; }
And then add the following to your view:
#Html.RadioButtonForEnum(m => m.AnswerType)
Hope this helps!
this will work
Radio button for model
#using (Html.BeginForm())
{
foreach (var department in Model.Departments)
{
#Html.RadioButtonFor(m => m.SelectedDepartment, department.Id) #department.Name
}
<input type="submit" value="Submit" />
}
In your viewmodel you could have a public int SelectedRating. You could make it a string and change the values from 114... to string values as well.
<div class="radio">
<label>
#Html.RadioButtonFor(x => x.SelectedRating, 114)
<span>Not Sure</span>
</label>
</div>
<div class="radio">
<label>
#Html.RadioButtonFor(x => x.SelectedRating, 115)
<span>Agree</span>
</label>
</div>
<div class="radio">
<label>
#Html.RadioButtonFor(x => x.SelectedRating, 116)
<span>Don't Agree</span>
</label>
</div>

radiobuttonfor grouping razor mvc5

The selected values are not getting posted back when I group my radio buttons,
So, with this the SelectedQuestionId is blank
<div class="row">
<div class="col-md-4">
<div class="form-group">
#Html.HiddenFor(x => x.UserResponses[0].QuestionId)
#Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId, new { Name ="selectone"})
#Html.DisplayTextFor(x => x.UserResponses[0].QuestionText)
#Html.HiddenFor(x => x.UserResponses[0].QuestionText)
#Html.HiddenFor(x => x.UserResponses[0].InputType)
#Html.HiddenFor(x => x.UserResponses[0].Points)
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<span class="text-muted">#Html.DisplayTextFor(x => x.UserResponses[0].Points)</span> #Html.LabelFor(x => x.UserResponses[0].Points, new { #class = "text-muted" })
</div>
</div>
</div>
<div class="row">
<div class="col-md-4">
<div class="form-group">
#Html.HiddenFor(x => x.UserResponses[1].QuestionId)
#Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId, new { Name = "selectone"})
#Html.DisplayTextFor(x => x.UserResponses[1].QuestionText)
#Html.HiddenFor(x => x.UserResponses[1].QuestionText)
#Html.HiddenFor(x => x.UserResponses[1].InputType)
#Html.HiddenFor(x => x.UserResponses[1].Points)
</div>
</div>
<div class="col-md-4">
<div class="form-group">
<span class="text-muted">#Html.DisplayTextFor(x => x.UserResponses[1].Points)</span> #Html.LabelFor(x => x.UserResponses[1].Points, new { #class = "text-muted" })
</div>
</div>
</div>
But the moment i remove the grouping from the radiobuttonfor then all works well, and the SelectedQuestionId is populated with questionid. So, replacing the RadioButtonFor above with these allow the SelectedQuestionId to be populated when the form is posted.
The code above is based on this question and though I have asked a related question here - this question is entirely separate.
#Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId)
#Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId)
found a solution to this rather intractable problem.
#Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId)
#Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId)
with the code above each radiobutton had a unique name during the deserialization process the SelectedQuestionId property would be populated for a given item if it was selected as one would expect. However, for the radiobutton to belong to a group they would need to have the same name attribute and since this was not the case in the above scenario one could select both the radio buttons. Which also meant that with multiple radiobuttons being selcted selectedId property for all the selected radio buttons would be populated with their respective QuestionId.
To get around this one could always write code as so,
#Html.RadioButtonFor(x => x.UserResponses[0].SelectedQuestionId, Model.UserResponses[0].QuestionId, new { Name = "selectone"})
#Html.RadioButtonFor(x => x.UserResponses[1].SelectedQuestionId, Model.UserResponses[1].QuestionId, new { Name = "selectone"})
And, now one gets the ability to select only one radiobutton but then during the deserialization process in the HttpPost method the SelectedQuestionId property for each item in the collection would be null. Obviously, the name of the radiobuttons were now "selectone" and so in the key/value pairing for SelectedQuestionId the value would not be getting populated, in other words the SelectedQuestionId would always be null even for the radio button that was selected.
So, the solution was the following:
#Html.RadioButtonFor(x => x.SelectedId, Model.UserResponses[i].QuestionId)
x.SelectedId lies in the ParentModel, and gets populated with the QuestionId of the question that is selected and since both the radio button share the same name they therefore belong to the same group and hence only one of the radio buttons can be selected.

Resources