asp.net mvc Html.checkboxfor not reflecting values from model - asp.net-mvc-3

I am using MVC3 and in my view file I have put a Checkbox using below code. But Model binding is not working. Even if property is set as false the checbox is coming as checked.
<div class="editor-field">
#Html.HiddenFor(x=>x.IsDirectHire)
#Html.CheckBoxFor(model => model.IsDirectHire)
#Html.ValidationMessageFor(model => model.IsDirectHire)
</div>
Please help me find a solution

Related

Kendo Grid - Customize Edit Popup

I'm using Kendo Grid with several columns which are used for overview row data. When users click Add/Edit buttons, the popup will be shown with some additional data which includes some checkboxes.
I have a problem when binding the checkboxes with the current MVVM model because, when adding a new row, Kendo treats the model as a variable, not an array. This causes many checkboxes to be checked when one is checked(clicked). After taking a look at Kendo MVVM, I intended to get the MVVM model of current popup in order to manipulate some data but was not successful. Therefore I would look for the help in:
Getting the current MVVM model of the popup (So that I can edit the model)
Any recommendation in binding many checkboxes when clicking the Add button(there is no initial data).
you need to write a template for this
then write this to the grid
.Editable(editable => editable.Mode(GridEditMode.PopUp).TemplateName("myTemplate"))
this is a sample template:
#model teknik.Models.Magaza_Viewmodel
#Html.HiddenFor(model => model.ID)
<div class="editor-label">
#Html.LabelFor(model => model.ADI)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ADI)
#Html.ValidationMessageFor(model => model.ADI)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.ADRES)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.ADRES)
#Html.ValidationMessageFor(model => model.ADRES)
</div>

MVC3 and validating a mixture of Razor and jQuery plugins

I am using MVC3 and my view has a mixture of razor and raw html in my view e.g.
<div class="editor-field">
#Html.EditorFor(model => model.PAGE)
#Html.ValidationMessageFor(model => model.PAGE)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.PERIODICAL)
</div>
div class="editor-field">
<div class="editor-field">
<input type="text" id="periodicallist" name="tiperiodical" />
</div>
</div>
My JavaScript applies the tokenInput control plugin to the textbox and its all fine, but I wonder how I can validate this (i.e. for empty textbox/tokenInput), it needs to filled with at least 1 token. At the moment it's confusing having these two forms of creating a view, letting Razor constuct elements direct from the model, and then adding more complexity/cusomization by usin jQuery plugins etc.
Can anyone advise how I can validate jQuery plugins on posting back to the controller?
Cheers
If you are using unobtrusive validation then you all have to do is add the necessary HTML5 data attributes to the input elements from javascript and microsoft's unobtrusive javascript library will take care of the rest.
For ex. if you have dynamically injected a textbox into the form through javascript and you need to perform required validation then all you have to add the data-val and data-val-required attributes as below.
<input data-val="true"
data-val-required="No. of joinees is required"
name="NoOfJoinees"
type="text" />
EDIT:
To display the validation error message you have to inject a span near to the field,
<span class="field-validation-valid"
data-valmsg-for="NoOfJoinees"
data-valmsg-replace="true">
</span>

ASP.NET MVC 3 Partial View Template

To be honest, I have no idea what to call this or how to start to search it.
I have a display page with a standard layout.
<div>
<label for="field">Field Name:</label>
#Model.Field
</div>
While trying to make this more change friendly, I want to make a template instead of typing out each field with the above code.
I created a partial view with the following:
#model System.Object
<div>
#Html.LabelFor(m => m)
#Html.DisplayFor(m => m)
</div>
On my view, I added the following:
#Html.Partial("_BillField", Model.Field)
The model then has a description with like:
public ModelName {
[Display(Name="Field Description")]
public decimal Field { get; set; }
}
This works when on the main view, but the label is missing when using the template. What am I missing?
Update: Per #K. Bob I make the change to the partial view:
<div>
#Html.LabelFor(m => m)
#Html.DisplayFor(m => m)
</div>
Update 2: For clarity of what I want.
In the end, I want to be able to do:
#Html.Partial("_BillField", Model.Field1)
#Html.Partial("_BillField", Model.Field2)
#Html.Partial("_BillField", Model.Field3)
And have the equivalent of:
<div>
<label for="field1">Field 1 Name:</label>
#Model.Field1
</div>
<div>
<label for="field2">Field 2 Name:</label>
#Model.Field2
</div>
<div>
<label for="field3">Field 3 Name:</label>
#Model.Field3
</div>
Sorry for not making that clearer.
The partial doesn't need told what the #model is, it'll use the parent #model, if you take out the #model in the partial does that help?
If I have this as the view....
#model MyApp.Models.ModelName
#{
ViewBag.Title = "Test";
}
<h2>Test</h2>
<div>
<div class="editor-label">
#Html.LabelFor(m => m.Field)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.Field)
#Html.ValidationMessageFor(m => m.Field)
</div>
</div>
#Html.Partial("_partial", Model) #*note I pass the whole model*#
And this as the partial....
#model MyApp.Models.ModelName
<div>
<div class="editor-label">
#Html.LabelFor(m => m.Field)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.Field)
#Html.ValidationMessageFor(m => m.Field)
</div>
</div>
Then it does what I think you want to do (obv. it does it twice but you could remove the code from the main view).
I'm not sure it gives you a huge benefit though. Maybe I've misunderstood something.
I think that potentially you should use Display/Editor templates for types, your model includes 3 fields of that type.
#Html.Partial("_BillField", Model.Field1)
#Html.Partial("_BillField", Model.Field2)
#Html.Partial("_BillField", Model.Field3)
Defining a Template for the type rather than a Partial view is possibly more effective. See ASP.NET MVC 3 - Partial vs Display Template vs Editor Template for a detailed comparison.
In this case your View would look more like:
#Html.DisplayFor(model => model.Field1)
#Html.DisplayFor(model => model.Field2)
#Html.DisplayFor(model => model.Field3)
Your model would be:
public class model
{
[DisplayName("Field1")]
public ComplexType Field1 {get;set;}
[DisplayName("Field2")]
public ComplexType Field2 {get;set;}
[DisplayName("Field3")]
public ComplexType Field3 {get;set;}
}
Or whatever the data annotation is for the display name.
Sounds like you want a DisplayFor(m => m):
http://buildstarted.com/2010/09/10/overriding-displayfor-and-editorfor-to-create-custom-outputs-for-mvc/
Function reference: http://buildstarted.com/2010/09/29/htmlhelper-guide-for-mvc-3-part-2/
You don't need to ToString() your property.
#Html.LabelFor(m => m)
UPDATED
Based on what you want to do, re-use views, take a look at this longer than normal post about reuse of validation and partial views I wrote up to the answer for ASP.NET MVC 3 - Model Validation. It is extremely detailed.
Added a new answer as the other one didn't answer the question once clarified.
This post LabelFor extension should be able to be adjusted to suit your needs I think, but rather than override better to create your own extension.
You'll need to call it slightly differently from how you've proposed because you need to use the m=>m.Field syntax. But I'm sure this should do what you need without having to use a partial view.
If you use Model.Field syntax you will only ever send in the actual value of the Field property, like 1.23 for your decimal, you need to use the m=>m.Field to get more than just the evaluation of the property so that you can change the text in the label.

How to keep model contents across View call

I have a Model that is being used in a view which can edit the model. A foreign key in the model is set before displaying the view, and is not touched in the view. The View shows a drop down for that field if it is zero, or skips the drop down if it is not zero.
#if (Model.RepairOrderId == 0)
{
<div class="editor-label">
#Html.LabelFor(model => model.RepairOrderId)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.RepairOrderId, Model.Orders)
#Html.ValidationMessageFor(model => model.RepairOrderId)
</div>
}
else
{
}
When the HTTP POST controller method is called the model contains all the fields that were edited, but the foreign key property that wasn't touched is now empty. What should I put in the else block to keep the non-zero RepairOrderId?
#Html.HiddenFor(model => model.RepairOrderId)
This generates a hidden input in your html, this way the modelbinder will pick up the value and set it in your viewmodel.

Problem with dropdown in Razor

I have 2 table in db: MixedType(id and name) and Block(id, name, idMixedType).
I want to make strongly-typed view for Block (Create view).
Controller is following:
public ActionResult Create()
{
return View();
}
Block() is a partial class (I use Entity Framework + POCO).
I have no problem with text fields, it works fine:
<div class="editor-label">
#Html.LabelFor(model => model.name)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.name)
#Html.ValidationMessageFor(model => model.name)
</div>
But I want to make dropdown for idMixedType field with values from MixedType table.
I tried to do it in following way (according to this answer Create a Dropdown List for MVC3 using Entity Framework (.edmx Model) & Razor Views && Insert A Database Record to Multiple Tables):
<div class="editor-label">
#Html.LabelFor(model => model.idMixedType)
</div>
<div class="editor-field">
#Html.DropDownListFor(model => model.idMixedType, new SelectList(Model.MixedType, "id", "name"))
#Html.ValidationMessageFor(model => model.idMixedType)
</div>
But I have a error
The best overloaded method match for 'System.Web.Mvc.SelectList.SelectList(System.Collections.IEnumerable, string, string)' has some invalid arguments
What is wrong?
You're passing in Model.MixedType to the SelectList constructor. Presumably Model.MixedType is not IEnumerable.
Is it possible it should be a lowercase "m" (model.MixedType)?
If not, you need to review the static MixedType property and make sure it is a collection that implements IEnumerable (and that the objects it enumerates have "id" and "name" properties, but I presume that's the case).

Resources