DisplayName not being displayed by DisplayFor helper - asp.net-mvc

In my ASP MVC ViewModel, I have the following properties that are using the DisplayName identifier
[DisplayName("Payment Frequency")]
public char paymentFrequency { get; set; }
[DisplayName("Account Type")]
public char accountType { get; set; }
However when they are called by the DipslayFor helper in the view like so
<div class="M-editor-label">
#Html.DisplayFor(model => model.paymentFrequency)
</div>
<div class="M-editor-label">
#Html.DisplayFor(model => model.accountType)
</div>
I get a big ol' blank space. Any idea why?

The DisplayName is the Label Display value ->
use #Html.LabelFor(m => m.paymentFrequency)
you're getting blanks because you have empty properties i'm guessing.
Usually you use LabelFor and DisplayFor with eachother to display the label and the value.

Related

ASP.NET MVC Details view with many display fields but a single editable field

I have a view that shows a single item with all of its fields. But I'm getting confused trying to figure out how to allow one specific field ("Status") to be updated from this view, without necessarily going into a whole other "edit" view first.
How do I http-post to the same action (Details) but saving only the "Status" field, without saving all of the other properties which are part of the view model (for display only)? Do I need a separate view model just for the Status? New to ASP.NET MVC and getting confused.
View Model
public string FirstName { get; set; }
public string LastName { get; set; }
public string Birthdate{ get; set; }
public string Status { get; set; }
//etc.
View
<div>
#Html.DisplayFor(model => model.FirstName)
#Html.DisplayFor(model => model.LastName)
#Html.DisplayFor(model => model.Birthdate)
//etc.
</div>
<div>
#TextBoxFor(model => model.Status)
<button type="submit" value="Save Status" />
</div>
Controller
[HttpGet]
public ActionResult Details(int id)
{
var person = personRepo.GetById(id);
var vm = BuildPersonDetailsViewModel(person);
return View(vm);
}
[HttpPost]
public ActionResult Details(PersonDetailsViewModel vm)
{
var person = personRepo.GetById(vm.PersonID);
person.Status = vm.Status;
personRepo.Update(person);
}
So I solved this by ensuring that the primary key field PersonID is included in the View. I didn't think I needed it originally because it started off as a read-only Details view, and PersonID wasn't needed to be displayed. But when posting data back, I needed to add it as a hidden field, so it could be passed to the controller on HttpPost. Then it can be used to locate and update the record in the database.
Furthermore, I added another method in the repository to allow for updating of just the "Status" field, since that's the only value being updated. If I use the sample code above, my solution looks something like this:
View Model
public int PersonID { get; set; }
public string Status { get; set; }
public string FirstName { get; set; }
//etc.
View
#Html.HiddenFor(model => model.PersonID)
#Html.DisplayFor(model => model.FirstName)
//etc. (all the display fields)
#TextBoxFor(model => model.Status)
<button type="submit" value="Save Status" />
Controller
[HttpPost]
public ActionResult Details(PersonDetailsViewModel vm)
{
personRepo.UpdateStatus(vm.PersonID, vm.Status);
}
This is probably not the case but since i can't tell from the sample code, do you have the Html.BeginForm(){} block around the model that you are trying to post? Also maybe try changing the <button> tag to <input type='submit' value='save status'/> instead

Set value from model in a label using html helper

I have given view model.I have to set the value of Amount(which is getting retrieved from table using linq) on a label using Html helper.
public class AllocationViewModel
{
public long ID { get; set; }
public string Name { get; set; }
public double Amount { get; set; }
}
Code for view page:--
#model Assetry.Controllers.AllocationViewModel
#using (Html.BeginForm("Index", "Deal", FormMethod.Post))
{
#Html.DisplayFor(model => model.Amount)
}
Something like this maybe?
#model AllocationViewModel
#Html.DisplayFor(model => model.Amount)
Try this,
#Html.Label(Model.Amount)
or
IF you want value in model,
#Html.LabelFor(model => model.Amount)
I think in your case you don't need a display template, except you want to represent a double in a sophisticated manner using a template.
Try something like that (if you want to just display the amount):
<span>#Model.Amount</span>
To edit:
#Html.LabelFor(m => m.Amount)
#Html.TextBoxFor(m => m.Amount)

Refer to property within collection in MVC view, using Razor

I have a model that looks somewhat like this:
public class MyClass {
public string Id { get; set; }
public List<SubItem> SubItems { get; set; }
}
public class SubItem {
public string Key { get; set; }
public string Value { get; set; }
}
In my view, I want to submit form data to MyClass, so I can create an object of MyClass. It looks like this:
#model Models.MyClass
#using (Html.BeginForm()){
<div>
#Html.DisplayFor(model => model.Id): #Html.EditorFor(model => model.Id)
</div>
<div>
#Html.DisplayFor(model => ???): #Html.EditorFor( ??? )
</div>
<input type="submit" value="create"/>
}
You see the question marks (???) where I am in doubt. How do I get to add to this collection? I know it is a sub form of sorts, but how do I do it without much complication. If I needed to show the items, I would do a foreach(var item in Model.SubItems) { ... }. But this is different. How do I handle this?
It's really not different than displaying each item individually:
#for (int i=0; i<Model.SubItems.Length; i++)
{
<div>
#Html.DisplayFor(m => m.SubItems[i].Key): #Html.EditorFor(m => m.SubItems[i].Key)
</div>
<div>
#Html.DisplayFor(m => m.SubItems[i].Value): #Html.EditorFor(m => m.SubItems[i].Value)
</div>
}
UPDATE
Changed code above to make sure names and index values are correctly generated. Also, this will now work with scenario of no initial items, as well. Just change the i<Model.SubItems.Length condition to i<3, or whatever number of iterations you'd like.

Get DisplayAttribute of Parent Property

I have a View Model that is defined as follows:
public class VariableViewModel
{
public string TemplateName { get; set; }
public bool Enabled { get; set; }
public string Value { get; set; }
}
I am using this model in other View models:
public class CreateViewModel
{
[Display(Name="First Name")]
public VariableViewModel FirstName { get; set; }
[Display(Name="Last Name")]
public VariableViewModel LastName { get; set; }
}
I have an editor template defined for VariableViewModel:
#model VariableViewModel
#Html.HiddenFor(model => model.TemplateName)
#Html.HiddenFor(model => model.Enabled)
#Html.LabelFor(model => model.Value)
#Html.TextBoxFor(model => model.Value)
An editor template for my CreateViewModel:
#model CreateViewModel
#Html.EditorFor(model => model.FirstName)
#Html.EditorFor(model => model.LastName)
Right now my editor template is creating the label as follows:
<label for="FirstName">Value</label>
Is it possible to modify LabelFor in a way that it looks at the DisplayAttribute of the parent property to determine what it should use instead of having it be Value? I want my labels to look like:
<label for="FirstName">First Name</label>
<label for="LastName">Last Name</label>
The problem is that the Display attribute is not on the property I am creating the label for, but on the containing object. Is there a way to accomplish what I want?
In your editor template simply use the following for the label:
#Html.LabelFor(model => model.Value, ViewData.ModelMetadata.DisplayName)
We are passing the parent display name as value for the label.

Using System.ComponentModel.DataAnnotations with Entity Framework 4.0

I'm working with MVC3, and using Entity Framework 4.0 Entities as my model. So far, everything works great as far as using it as a model (all the crud operations/page generations work out of the box). I'm wondering, though, how do you get the same robust labels and validation information as when you generate a model manually?
Here's an example of what I mean. This is a class generated by the sample MVC3 project:
public class LogOnModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
With the example above, you can specify what gets rendered in a label for the field (Display), and what type of field to use (Password). However, when I try to use the entity framework and push it to the view below, I see the automatically generated labels are just the field names, and not anything I want the user to see/have to read:
#using (Html.BeginForm()) {
#Html.ValidationSummary(true)
<fieldset>
<legend>Person</legend>
<div class="editor-label">
#Html.LabelFor(model => model.FirstName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.FirstName)
#Html.ValidationMessageFor(model => model.FirstName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.MiddleName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.MiddleName)
#Html.ValidationMessageFor(model => model.MiddleName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.LastName)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.LastName)
#Html.ValidationMessageFor(model => model.LastName)
</div>
<div class="editor-label">
#Html.LabelFor(model => model.Birthdate)
</div>
<div class="editor-field">
#Html.EditorFor(model => model.Birthdate)
#Html.ValidationMessageFor(model => model.Birthdate)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>}
My question is: How do I add these extra decorations to the entities that are generated using EF4? Is there something besides System.ComponentModel.DataAnnotations that I should be using? I know entities get regenerated and it's probably not a good idea to add this to entities' code directly, but for some reason I can't think of a better approach than manually entering the label text in the view (lame, there's no reason to have to do that, this is MVC!). I want to keep it so that the application is dynamic enough to be able to have the correct display information for my model come through and keep an MVC approach. How do I do it?
I haven't done this for ASP.NET MVC (only for Silverlight) but I believe the same principles would apply. You can create a "metadata buddy class" as below, because the types generated by EF should be partial, thus you can add a bit more to them (like the MetadataTypeAttribute) and then you create this sibling class that holds the metadata.
It's kind of ugly, but should work. It goes something like this (assuming the EF entity is named "Person"):
[MetadataType(typeof(PersonMetadata))]
public partial class Person {
// Note this class has nothing in it. It's just here to add the class-level attribute.
}
public class PersonMetadata {
// Name the field the same as EF named the property - "FirstName" for example.
// Also, the type needs to match. Basically just redeclare it.
// Note that this is a field. I think it can be a property too, but fields definitely should work.
[Required]
[Display(Name = "First Name")]
public string FirstName;
}
Same as above but with all the details, and it works
And Here is the Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel.DataAnnotations;
namespace Validate.Models
{
[MetadataType(typeof(PersonMetadata))]
public partial class Person
{
// Note this class has nothing in it. It's just here to add the class-level attribute.
}
public class PersonMetadata
{
// Name the field the same as EF named the property - "FirstName" for example.
// Also, the type needs to match. Basically just redeclare it.
// Note that this is a field. I think it can be a property too, but fields definitely should work.
[Required]
[Display(Name = "Enter Your Name")]
public string FirstName;
}
}
Like Austin Lamb's answer, but instead, nesting the MetaData class within the entity class, thereby reducing the number of classes in your public namespace list, and eliminating the need to have a unique name for each metadata class.
using System.ComponentModel.DataAnnotations;
namespace Validate.Models
{
[MetadataType(typeof(MetaData))]
public partial class Person
{
public class MetaData
{
[Required]
[Display(Name = "Enter Your Name")]
public string FirstName;
//...
}
}
}

Resources