How to disable model view check box in .net mvc? - asp.net-mvc

How to disable model view check box in .net mvc?
I tried with the syntax : new { disabled = "disabled" } but doesn't work.
<div class="col-sm-7 checkbox-inline text-left">
<label style="margin-right:15px"></label>
#Html.CheckBoxFor(model => model.Email, new { disabled = "disabled" } ) <label class="hthin" asp-for="Email" style="margin-right:30px"></label>
#Html.CheckBoxFor(model => model.Pager, new { disabled = "disabled" }) <label class="hthin" asp-for="Pager" style="margin-right:30px"></label>
#Html.CheckBoxFor(model => model.Landline, new { disabled = "disabled" }) <label class="hthin" asp-for="Landline" style="margin-right:30px"></label>
</div>

#Html.CheckBoxFor() - model object property should be boolean.
Thanks Stephen Muecke. I updated my answer. # symbol is used for reserved keywords. For example #class.
Thanks GregH. You catch it perfect.

Related

Disable autocomplete option on Google Chrome using MVC 5 application

We tried to use Autocomplete attribute of textbox and dynamic value on name attribute of textbox but unable to disbale autocomplete option on Google Chrome but it work perfectly fine on IE, Firefox.
You can try this way
If the input is inside the form, you should do like this:
<form autocomplete="off">
And the input you should do
<input type="text" id="input_name" autocomplete="none"/>
This is a sample block using Razor
<div class="position-relative form-group">
#Html.LabelFor(m => m.EmailAddress, new { #class = "control-label" })
#Html.TextBoxFor(m => m.EmailAddress, new { #class = "form-control", #autocomplete = "off" })
#Html.ValidationMessageFor(m => m.EmailAddress, "", new { #class = "text-danger", #style = "font-style:italic;" })
</div>
Hope this one helps

MVC parial view going to get method rather than post on submit

I have a partial view which shows a search box with a couple of options
model PatientSearchViewModel
#using (Html.BeginForm("Index", "Patient", FormMethod.Post, new { #class = "form-inline" }))
{
<form class="form-inline">
<label style="padding-right:20px;">Search for a patient</label>
#Html.EditorFor(model => model.SearchText, new { htmlAttributes = new { #class = "form-control", placeholder = "Search...", autocomplete = "off" } })
<div class="checkbox">
<div class="checkbox" style="padding-left:20px; padding-right:20px;">
<label>#Html.EditorFor(model => model.ShowOnlyOpenClients) Show only open clients</label>
</div>
</div>
<button type='submit' name='seach' id='search-btn' class="btn btn-primary"><i class="fa fa-search"></i>Search</button>
</form>
}
When the submit button is hit I want it to go to my method below with the search content already complete, however instead it goes to the HttpGet method which is bringing back a null for the PatientSearchViewModel.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Index(PatientSearchViewModel patient)
{
PatientSearchViewModel patientVM = GetSearchResults(patient);
return View(patientVM);
}
Can anyone explain how I can get this to work?
I have executed the code snippet you pasted here and found that
As you are making the use of the [ValidateAntiForgeryToken]
attribute so you need to add the #Html.AntiForgeryToken(); this
statement in your partial view inside the
#using(Html.BeginForm()){ } block.
One more thing make sure that your action method will be inside the
PatientController.
For your reference (this is my code)
#model WebApplication.Models.PatientSearchViewModel
#using (Html.BeginForm("Index", "Patient", FormMethod.Post, new { #class = "form-inline" }))
{
#Html.AntiForgeryToken();
<label style="padding-right:20px;">Search for a patient</label>
#Html.EditorFor(model => model.SearchText, new { htmlAttributes = new { #class = "form-control", placeholder = "Search...", autocomplete = "off" } })
<div class="checkbox">
<div class="checkbox" style="padding-left:20px; padding-right:20px;">
<label>#Html.EditorFor(model => model.ShowOnlyOpenClients) Show only open clients</label>
</div>
</div>
<button type='submit' name='seach' id='search-btn' class="btn btn-primary"><i class="fa fa-search"></i>Search</button>
}
Suggestion: As you have used the #using(Html.BeginForm()) which will any way going to convert to <form> tag so instead of using another <form class="form-inline"> inside it is creating the confusion please you may remove it.
I hope this will solve your problem.

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.

Value of TextBox with Disabled propertie isn't pass to controller

If I put a disabled='disabled' propertie in a TextBox with Razor, when I submit my form, the value of this TextBox isn't sent to the controller.
#using (Html.BeginForm("Principal", "Home")) {
<div class="form-group">
#Html.LabelFor(m => m.Fornecedor, new { #class = "col-sm-2 control-label" })
<div class="col-sm-9">
#Html.TextBox("Fornecedor", Model.Fornecedor, new { #class = "form-control", type = "text", disabled = "disabled" })
</div>
</div>
}
When submitted, the Model.Fornecedor value in controller is null, but without the disabled propertie, the value return correctlly.
Can someone help me, to put a TextBox field disabled but pass his default value to controller when the form is submitted ?

MVC - Showing validation message in place of Label

Suppose this is my view...
<div class="control-group">
#Html.LabelFor(model => model.Title, new { #class = "control-label" })
#Html.ValidationMessageFor(model => model.Title, null, new { #class = "help-inline" })
<div class="controls">
#Html.EditorFor(model => model.Title)
</div>
</div>
In case of error How we can hide the Label and display validation message in place of Label?
Please suggest on this...
You could display the Label only if the ModelState is valid:
<div class="control-group">
#if (this.ViewData.ModelState.IsValid)
{
#Html.LabelFor(model => model.Title, new { #class = "control-label" })
}
#Html.ValidationMessageFor(model => model.Title, null, new { #class = "help-inline" })
<div class="controls">
#Html.EditorFor(model => model.Title)
</div>
</div>
Notice that if you are using client side validation you will also need to hook up to the validate method on the client and toggle the corresponding label visibility as well.
Just to compliment Darin's answer, if you are using client-side validation you will need to do something like
$("#myForm").validate({
invalidHandler: function(event, validator) {
$('.control-label').hide();
}
});
Otherwise the label won't hide.
It's worth still checking ModelState.IsValid when the page loads as well though incase the user has JS disabled.

Resources