Set value from model in a label using html helper - asp.net-mvc

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)

Related

DisplayName not being displayed by DisplayFor helper

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.

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.

How to get the value from a kendo Editor in my model

I am trying to use a Kendo UI Editor control in my ASP.NET MVC application. No success until now, since I cannot manage to get the value in the editor back to the model in the controller.
My model is very simple (to edit an html page in my website):
public class EditedPage
{
public string Name { get; set; }
public string Title { get; set; }
[AllowHtml]
public string Content { get; set; }
}
And my view includes this code:
#model Page
<h2>#Model.Title</h2>
#using (Html.BeginForm())
{
#Html.HiddenFor(m => m.Name)
#Html.HiddenFor(m => m.Title)
#(Html.Kendo().EditorFor(m => m.Content)
.Name("Editor")
.HtmlAttributes(new { style = "width:800px;height:600px;margin-left:20px;" })
)
<div>
<input type="submit" value="#Resources.StringResources.Save" class="k-button"/>
</div>
}
I was expecting the post method in the controller to get the model filled. If I add simple editors for Name and Title (in the sample code they are hidden) it works fine, but Content always comes back as null.
Here is my controller method:
[HttpPost]
public ActionResult EditPage(Page page)
{
if (!ModelState.IsValid)
return View(page);
//save content in a file
return View("CustomPages");
}
What am I missing? I guess that I need some javascript to get the value from the editor, but I don't know how to achieve it.
Any help would be welcome. Thanks
Name your editor 'Content'. Really. :)
EDIT
#model Page
<h2>#Model.Title</h2>
#using (Html.BeginForm())
{
#Html.HiddenFor(m => m.Name)
#Html.HiddenFor(m => m.Title)
#(Html.Kendo().EditorFor(m => m.Content)
.Name("Content")
.HtmlAttributes(new { style = "width:800px;height:600px;margin-left:20px;" })
)
<div>
<input type="submit" value="#Resources.StringResources.Save" class="k-button"/>
</div>
}
I had the same issue, and the only way I could get this resolved when using and EditorFor was to not populate the Name property at all.

Multiple partial views based on same model

I have something like this:
Main view:
#model AuthorViewModel
#using (Html.BeginForm("Action", "Controller", FormMethod.Post, new { id="someId" })) {
#Html.LabelFor(model => model.Name);
#Html.EditorFor(model => model.Name);
#Html.ValidationMessageFor(model => model.Name);
<label> Book </label>
#{Html.RenderPartial("_BookView", new BookViewModel());}
<label>One more book...</label>
#{Html.RenderPartial("_BookView", new BookViewModel());}
}
Partial view:
#model BookViewModel
#Html.LabelFor(model => model.Title);
#Html.EditorFor(model => model.Title);
#Html.ValidationMessageFor(model => model.Title);
AuthorViewModel:
public class AuthorViewModel
{
[Required]
[DataType(DataType.Text)]
public String Name { get; set; }
}
BookViewModel:
public class BookViewModel
{
[Required]
[DataType(DataType.Text)]
public String Title { get; set; }
}
So when it renders - it looks right, but validation is the same for all books. An I need to have a lot of books(say to add them dynamically) for author and each one have to be independent and "validatable".
How can I perform such behaviour?
I would have a collection of BookViewModel in your AuthorViewModel. That way the names and ids will be unique.
You could update your AuthorViewModel to have a List of BookViewModel. In the View, iterate over the list and create the necessary fields for the booktitles.
You're trying to model bind to a list.
Its pretty simple to implement, have a look at Phil Haacks post here.
He uses the old mvc views, but the same idea works fine for razor

How to have one RadioButtons group display in EditorTemplates in ASP.Net MVC(3)

I have a list of items from which I want the user to be able to input some value select one
But the radio-buttons generated by the EditorTemplate are named like "Item[x].SelectedItemId" so they are totally independent from each other and I can't get the value...
Let's go show some code.
The model:
public class FormModel
{
public List<ItemModel> Items { get; set; }
public int SelectedItemId { get; set; }
}
public class ItemModel
{
public int ItemId { get; set; }
public string ItemName { get; set; }
public string SomeString { get; set; }
}
The view:
#model FormModel
#using (Html.BeginForm())
{
#Html.EditorFor(m => m.Items)
}
The editor template:
#model ItemModel
#Html.RadioButton("SelectedItemId", Model.ItemId)
#Model.ItemName <br/>
#Html.TextBoxFor(m => m.SomeString) <br/>
UPDATE
This is what I want:
This is what I get:
As a result, FormModel.SelectedItemId never gets the value of any radio-button.
What am I doing wrong?
It appears as though you are aware that setting the names for radio buttons to be the same is necessary to make them work. However, when you do that in an editor template by using the line of code #Html.RadioButton("SelectedItemId", Model.ItemId), MVC 3 will take into consideration that you are in an editor template for Items and prepend items[n].
This would create a name of something like name="Items[0].SelectedIndex". This would be fine if it weren't for the fact that the next radio button would be `name="Items[1].SelectedIndex".
One way to solve this is to not use an editor template and use a foreach loop instead. Here is some code that I was able to get functional. I confirmed that model-binding worked for the SelectedIndex.
#model FormModel
#{
ViewBag.Title = "Index";
}
#using (Html.BeginForm())
{
foreach (var item in Model.Items)
{
#Html.RadioButtonFor(x => x.SelectedItemId, item.ItemId)
#item.ItemName <br/>
#Html.TextBoxFor(m => item.ItemName) <br/>
}
<input type="submit" value = "submit" />
}
I had same problem, and we solved it with this piece of code.
#Html.RadioButton("", Model.Id, Model.Selected, new { Name = "deliveryMethod" })
You need to put Name property explicitly, so it will be used instead name that you get after EditorFor executes.

Resources