ASP.NET MVC: Using EditorFor() with a default template for enums - asp.net-mvc

I've written an EnumDropDownFor() helper which I want to use in conjunction with EditorFor(). I've only just started using EditorFor() so am a little bit confused about how the template is chosen.
My Enum.cshtml editor template is below:
<div class="editor-label">
#Html.LabelFor(m => m)
</div>
<div class="editor-field">
#Html.EnumDropDownListFor(m => m)
#Html.ValidationMessageFor(m => m)
</div>
Short of explicitly defining the template to use, is there any way to have a default template which is used whenever an Enum is passed in to an EditorFor()?

You may checkout Brad Wilson's blog post about the default templates used in ASP.NET MVC. When you have a model property of type Enum it is the string template that is being rendered. So you could customize this string editor template like this:
~/Views/Shared/EditorTemplates/String.cshtml:
#model object
#if (Model is Enum)
{
<div class="editor-label">
#Html.LabelFor(m => m)
</div>
<div class="editor-field">
#Html.EnumDropDownListFor(m => m)
#Html.ValidationMessageFor(m => m)
</div>
}
else
{
#Html.TextBox(
"",
ViewData.TemplateInfo.FormattedModelValue,
new { #class = "text-box single-line" }
)
}
and then in your view simply:
#Html.EditorFor(x => x.SomeEnumProperty)

Related

How can I use EditorFor with ViewData as source?

I setup a ViewData on my Controller such as:
ViewData["Registrations_Services"] = GetServices(); // return a List<Services>
Then, I'd like to show it in my View. Before, I was iterating using the model:
#Html.EditorFor(m => m.Services)
But now how can I do the same from ViewData? Tried with:
#foreach (var item in ViewData["Registrations_Services"] as IList<MyProject.Models.Services>)
{
#Html.EditorFor(item)
}
But the type arguments are different and of course it doesn't works.
Tried also:
#Html.EditorFor(m => ViewData["Registrations_Services"] as IList<MyProject.Models.Services>)
But it says
Templates can be used only with field access, property access, single-dimension array index, or single-parameter custom indexer expressions.
I need to apply the editor template Services.cshtml for each item:
<div class="form-group">
#Html.Label(Model.Description, htmlAttributes: new { #class = "control-label col-md-2" })
<div class="col-md-6 checkbox">
#Html.HiddenFor(m => m.Id)
#Html.HiddenFor(m => m.Name)
#Html.HiddenFor(m => m.RegistrationId)
#Html.HiddenFor(m => m.Description)
#Html.EditorFor(m => m.Enabled)
</div>
<div class="col-md-4"></div>
</div>
That's why I was using #Html.EditorFor(m => m.Services).
You can use the non-strong typed version, using the name of the ViewData key
#Html.Editor("Registrations_Services")
Note that this will generate
<input name="Registrations_Services[0].Id" ... />
<input name="Registrations_Services[0].Name" ... />
....
<input name="Registrations_Services[1].Id" ... />
<input name="Registrations_Services[1].Name" ... />
etc (i.e. prefixed with the Viewdata key), so the POST method needs to include a parameter with the same name as the ViewData key to be correctly bound
IEnumerable<Services> Registrations_Services
Having said that, you are editing data, so you should always use a view model, not data models in your view, and the view model would contain a IEnumerable<Services> Services property, making your use of ViewData unnecessary.

MVC - Editing a list of objects

I have the following class layout in MVC:
public class ReportModel
{
List<SomeItem> items;
string value;
string anotherValue;
}
now I create a strongly typed view in MVC of this type and make editable text fields to edit each value as well as use a foreach loop to populate text fields to edit the items in the list of someitem.
when I submit to the httppost method the singular values come back fine in the reportmodel object but the list does not get returned in the object. How should this be done?
When I say httppost I am referring to the method that MVC is posting back to
[HttpPost]
public ActionResult EditReport(ReportModel report)
{
// Save the report in here after the update on the UI side
}
View code for posting the list of someitem
if (Model.items != null && Model.items.Count > 0)
{
for (int i = 0; i < Model.items.Count; i++)
{
<div class="editrow">
<div class="edititem">
<div class="editor-label">
#Html.LabelFor(m => m.items.ElementAt(i).propertyOne)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.items.ElementAt(i).propertyOne)
#Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyOne)
</div>
</div>
<div class="edititem">
<div class="editor-label">
#Html.LabelFor(m => m.items.ElementAt(i).propertyTwo)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.items.ElementAt(i).propertyTwo)
#Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyTwo)
</div>
</div>
<div class="edititem">
<div class="editor-label">
#Html.LabelFor(m => m.items.ElementAt(i).propertyThree)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.items.ElementAt(i).propertyThree)
#Html.ValidationMessageFor(m => m.items.ElementAt(i).propertyThree)
</div>
</div>
</div>
}
}
Don't use ElementAt(1) in your lambda expressions => this ruins your input field names. Please read the blog post that Kirill suggested you.
So you could use indexed access:
for (int i = 0; i < Model.items.Count; i++)
{
<div class="editrow">
<div class="edititem">
<div class="editor-label">
#Html.LabelFor(m => m.items[i].propertyOne)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.items[i].propertyOne)
#Html.ValidationMessageFor(m => m.items[i].propertyOne)
</div>
</div>
<div class="edititem">
<div class="editor-label">
#Html.LabelFor(m => m.items[i].propertyTwo)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.items[i].propertyTwo)
#Html.ValidationMessageFor(m => m.items[i].propertyTwo)
</div>
</div>
<div class="edititem">
<div class="editor-label">
#Html.LabelFor(m => m.items[i].propertyThree)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.items[i].propertyThree)
#Html.ValidationMessageFor(m => m.items[i].propertyThree)
</div>
</div>
</div>
}
Of course in order to have indexer access to the collection this assumes that your items property is declared as either List<SomeItem> or SomeItem[]. If it is an IEnumerable<SomeItem> it won't work. So simply change the type of this property on your view model.
Kirill's reference to Scott Hanselman's blog entry is correct, but you're reading it too narrowly. In the example shown, he passes the array to the action method, but it could just as easily be contained within the parent model as well. The same concept applies.
However, one thing to know is that the default model binder does not instantiate nested classes, so it will not create an instance of the List class, which means it will always be null. To fix this, you must instantiate the empty list class in the constructor.
This is only part of the problem, though as the data must be formatted in the correct way for the model binder to bind it. This is where Scott's blog post comes in, as it provides the format needed for the model binder to recognize the data as a list.
This is typically handled for you if you use an EditorTemplate and use Html.EditorFor(m => m.Items) and then have a SomeItem.cshtml EditorTemplate. This deals with the issues of collection item naming (so long as you also use strongly typed helpers in the template as well).

Html.DisplayFor not posting values to controller in ASP.NET MVC 3

I am using ASP.NET MVC 3 with Razor, below is a sample from my view code.
The user should be able to edit all their details, except the "EmailAddress" field. For that field only I have used Html.DisplayFor(m => m.EmailAddress).
But when this form gets posted, all the model properties are filled except the EmailAddress.
How do I get the email back in the model when posting? Should I have used some helper other than DisplayFor?
#using (Html.BeginForm()) {
#Html.ValidationSummary(true, "Account update was unsuccessful. Please correct any errors and try again.")
<div>
<fieldset>
<legend>Update Account Information</legend>
<div class="editor-label">
#Html.LabelFor(m => m.EmailAddress)
</div>
<div class="editor-field">
#Html.DisplayFor(m => m.EmailAddress)
#*#Html.ValidationMessageFor(m => m.EmailAddress)*#
</div>
<div class="editor-label">
#Html.LabelFor(m => m.FirstName)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.FirstName)
#Html.ValidationMessageFor(m => m.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(m => m.LastName)
</div>
<div class="editor-field">
#Html.TextBoxFor(m => m.LastName)
#Html.ValidationMessageFor(m => m.LastName)
</div>
<p>
<input type="submit" value="Update" />
</p>
</fieldset>
</div>
}
Please advise me on this.
you'll need to add a
#Html.HiddenFor(m => m.EmailAddress)
DisplayFor won't send anything in POST, it won't create an input...
By the way, an
#Html.HiddenFor(m => m.Id) // or anything which is the model key
would be usefull

Html.RenderAction in MVC with EntityFramework

My problem is when I try to render a view with two Html.RenderAction. It says: "The operation cannot be completed because the DbContext has been disposed".
I have Ninject configured in this way:
Bind<IUnitOfWork>().To<UnitOfWork>().InRequestScope;
But If I do in the default way...
Bind<IUnitOfWork>().To<UnitOfWork>()
there's no error.
I have to work with it in RequestScope (so I think), but how can I do it? It seems's that when second Html.RenderAction is called the previous DbContext it's disposed !
UPDATED:
This is the main view (summarized for brevity)
#model FoodAway.Model.Product
#Html.ValidationSummary(true)
<fieldset>
<legend>Producto</legend>
#using (Html.BeginForm())
{
<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>
#Html.HiddenFor(model => model.Id)
<p>
<input type="submit" value="Guardar" />
</p>
}
<fieldset>
<div class="editor-label">
#Html.LabelFor(model => model.Ingredients)
</div>
<div class="editor-field">
#{Html.RenderAction("IngredientsToRemoveList", "Ingredients");}
</div>
</fieldset>
<fieldset>
#{Html.RenderAction("IngredientsToAddList", "Ingredients");}
</fieldset>
</fieldset>
and his controller/action:
public ActionResult EditProduct(string name)
{
Product product = unitOfWork.ProductRepository.Get(i => i.Name ==name).FirstOrDefault();
if (product == null)
return HttpNotFound();
return View(product);
}
So, the error in DBContext is when I have this 2 RenderAction methods, the strange thing is if I have only 1 RenderAction there is no problem!!!!!
You need to enumerate the set before passing it to the view. This means that you query the database within the valid scope of the DbContext.
You can do this by using .ToArray() in your controller

How do I bind data from Telerik ComboBox to my data model

Why won't my Telerik ComboBoxFor bind my value and fill my ComboBox via AJAX?
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<div class="editor-field">
#Html.EditorFor(model => model.Name)
#Html.ValidationMessageFor(model => model.Name)
</div>
<div class="editor-field">
#(Html.Telerik().ComboBoxFor(model => model.VendorId)
.Name("ddlVendor")
.ClientEvents(events =>
{
events.OnLoad("onVendorLoad");
//events.OnChange("onVendorChange");
events.OnDataBinding("onComboBoxDataBinding");
}
)
.DataBinding(bind => bind.Ajax().Select("_AjaxGetVendors", "Car"))
)
</div>
<p>
<input type="submit" value="Зберегти" />
</p>
</fieldset>}
In my controller I get entity but VendorId == 0.
[HttpPost]
public ActionResult Create(Car obj)
{
dm.InsertModel(obj);
return RedirectToAction("Create");
}
Option 1
Remove .Name("ddlVendor") from your ComboBox if you don't need it.
Option 2
Rename your ComboBox as follows and update any client event references to the control:
.Name("VendorId")

Resources