EditorTemplates/Object.cshtml using EditorFor() instead of Editor()? - asp.net-mvc

I am trying to create a generic editor template that replicates Html.EditorForModel(), to later customize and build upon. Brad Wilson's template gets pretty close, but I found that it chokes when the same key exist in both ViewData (or ViewBag) and the model. For example ViewBag.Title causes problems if the view model also has a Title property.
I learned here that using strongly-type helpers (i.e. Html.EditorFor(x => x.Title) instead of Html.Editor("Title") seems to help. So I tried to modify Brad's template, but I ran into a brick wall, as nothing I tried so far has worked. I can't figure out how to use strongly-typed helpers in a context where I don't know the model type, like an editor template for example.
Is there any way to create an Object template like Brad's, but using strongly-typed helpers (i.e. LabelFor, EditorFor, ValidatorMessageFor) instead of weakly-typed ones (i.e. Label, Editor, ValidatorMessage)?
Thanks.

I solved this problem in a slightly roundabout way, by removing the ViewData right before the call to #Html.Editor and then putting it back after.
Object.cshtml:
object oldViewData = null;
var hasConflictingViewData = ViewData.TryGetValue(prop.PropertyName, out oldViewData);
if (hasConflictingViewData)
{
ViewData.Remove(prop.PropertyName);
}
#Html.Editor(prop.PropertyName)
if (hasConflictingViewData)
{
ViewData.Add(prop.PropertyName, oldViewData);
}
The only other option I could think of is using a ton of reflection to call EditorFor generically with a runtime type, and pass in an expression for the pertinent property.

You can view all of the code for the new Object.shtml by going and downloading the MVC source code. I thought it was also in some common folder on your pc already but I can't remember where.
http://aspnet.codeplex.com/releases/view/58781

Related

Avoiding foreach for #html.checkboxfor

I am developing an application using MVC. I had a requirement where I have to display checkbox for a list.
I was going through different posts for doing this, one of them is the use of avoiding foreach for looping and making use of #html.editorfor() as described in the answer by Darwin dimitrov here:
This answer works fabulously fine, but I have a clarification , it is:
In the same view I have 2 requirements , the one with checkboxfor and the other one with radiobuttonfor
So, If I am using
<div>#Html.EditorFor(x => x.RoleAccess)</div>
How do I write the (~/Views/Shared/EditorTemplates/RoleAccessViewModel.cshtml) to serve for checkboxfor for one requirement , and the other one for #radiobuttonfor .
Wont this approach be hardcoded which will always render the RoleAccessViewModel.cshtml whenever EditorFor(x => x.RoleAccess) is used?Please execuse me If I have used any technical terms wrong way,as I still a novice in mvc.
The EditorFor method has an overload that accepts a template name as argument. I think that solves your problem if I understand it correctly. See http://msdn.microsoft.com/en-us/library/ee407414%28v=vs.118%29.aspx
You can also solve this by using the UIHint attribute on your property instead (or in addition to) relying on naming the template after your view model. Then you can create an alternate template to render the radio buttons and specify that:
[UIHint("RadioList")]
public List<Something> MyRadioButtonList { get; set; }
EditorFor will then look for the template: Views\Shared\EditorTemplates\RadioList.cshtml
You could do the same for your checkbox list, as well, instead of relying on the view model. For example, [UIHint("CheckboxList")] and CheckboxList.cshtml. Then, you'd be able to apply these templates more broadly.

Umbraco Razor - binding content fields

I've created a template (View) in Umbraco (MVC) and am trying to figure out how to bind to the document type content. Keeping it really simple:
#inherits Umbraco.Web.Mvc.UmbracoTemplatePage
#{
Layout = null;
}
<h1>#Model.Title</h1>
My Umbraco document type has a Title field (alias is 'title') but if I try and run this I get build errors. I've found a whole load of documentation suggesting using a Library.NodeById() method but I believe that's for WebForms and not MVC. Can anyone offer some guidance?
You can get a property value in multiple ways with Model::
#Model.Content.GetPropertyValue("title")
#Model.Content.GetProperty("title").Value
And as a dynamic
#CurrentPage.Title
Did you remember to add your template to your document type?
You can also use the Field helper method:
#Umbraco.Field("myFieldName")
nice thing about this helper is that you can also specify alternative fields (if the first one was empty.
You can find this back in the documentation:
http://our.umbraco.org/documentation/reference/templating/Mvc/views#RenderingafieldwithUmbracoHelper

Using #HTML.EditorForModel & DisplayForModel with ViewModels in MVC

Coming from a Silverlight background, MVVM is solidly planted in my mind. Moving to MVC, although rewarding, has been something of a paradigm shift for me.
One of the questions I have is about using EditorForModel when the strongly typed view has been passed a ViewModel with the object to be editted as a property, rather than the POCO being directly passed in as the model, with no viewmodel encapsulated around it.
How can I use EditorForModel on a property of the model, rather than the whole model itself? I went looking for something akin to #HtmlHelper.EditorForModel(model.Customer), but coulndt find an overload. It seems you can only generate an editor for the whole model...
(where customer is a good 'ol poco)
You could try -
#Html.EditorFor(m => m.Name)

Problem with IEnumerable in View

I have a view that enumerates over a model.
Outside of the grid that enumerates over the model, I want to have a create link that accepts a parameter of MeetingActionId, that will bind the ActionUpdate object to a specific MeetingAction.
How do I get the create link to accept that property? at the moment i get the error
'System.Collections.Generic.IEnumerable' does not contain
a definition for 'MeetingActionId' and no extension method 'MeetingActionId'
accepting a first argument of type
'System.Collections.Generic.IEnumerable' could be found
I assume it's something to do with the IEnumerable. I was going to solve it by putting the grid in a partial, but that seems like a hacky solution to me.
Can anyone assist, and hopefully educate me as to why this is happening?
Thank you.
The code snippet below is using the MVCContrib Grid, and T4MVC for strongly typed action links.
#model IEnumerable<Actioner.Models.ActionUpdate>
#using MvcContrib.UI.Grid
#{
ViewBag.Title = "ListUpdates";
}
<h2>ListUpdates</h2>
#Html.ActionLink("Add New Update",MVC.ActionUpdates.Create(Model.MeetingActionId))
#Html.Grid(Model).Columns(column => {
column.For(a=> a.UpdateText);
column.For(a=> a.CreatedOn).Format("{0:d}");
column.For(a=>a.CreatedBy);
})
EDIT: Thanks for the contributions guys. After some thinking i decided it would be better to have the grid of actionupdates rendered as a partial view within the 'details' view of the MeetingActions, thus avoiding this issue entirely.
This question could be closed
Hi
I'm not sure that I understand this correctly but it seems like the Model is a list that contains items of type ActionUpdate, that type has a property called MeetingActionId. If that is the case the Model itself does not have the property MeetingActionId that you are trying to use. Perhaps you could to this:
Model.First().MeetingActionId

lessons learned or mistakes made when using asp.net mvc

what are your top lessons learned when starting asp.net mvc that you would highlight to someone starting out so they can avoid these mistakes?
Use Html.Encode() everywhere you print data, unless you have a very good reason to not do so, so you don't have to worry about XSS
Don't hardcode routes into your views or javascripts - they're going to change at some point, use Url.Action() instead
Don't be afraid of using partial views
MVC is no silver bullet, first evaluate if it's indeed the best tool of choice for solving your problem.
Don't forget the "Unit Tests" part of the pattern.
Try to always use a ViewModel to pass data between the Controller and the View.
You may think you don't need one, you can just pass your model around, but suddenly you need a list box with several options for editing a model, or displaying a message (not validation message) and you start adding items to the ViewData, with magic strings as keys, making the app harder to maintain.
There are also some security issues that you solve with a ViewModel.
For instance:
class user:
int id
string name
string email
string username
string password
Your view let's the user change his name and email and posts to the action
public ActionResult Edit(User user)
{
--persist data
}
Someone could tamper your form and post a new password and username and you will need to be very careful with the DefaultBinder behavior.
Now, if you use a ViewModel like:
class userEditViewModel:
int id
string name
string email
The problem is gone.
Whenever it is possible make your view typed
Avoid logic in your views
stay away from the HttpContext
Get Steve Sandersons Pro ASP.NET MVC Framework
Debug into the Sourcecode
If you make a Controller method with a different parameter name from id for a single parameter method, you have to make a new route. Just bite the bullet and use id (it doesn't care about the type) and explain it in the comments.
Makes sure you name your parameters with RedirectToAction :
return RedirectToAction("DonateToCharity", new { id = 1000 });
You lose your ViewData when you RedirectToAction.
Put javascript in seperate files, not into the view page
name of the controller :)
unit test Pattern
Don't use the Forms collection, use model binding.
Try not to use ViewData, create a ViewModel.
If you have a loop or an if in your View, write an HTML helper.
Kindness,
Dan
Don't let your controller become a fat one and do too much work. I've seen 1000+ line controllers in the past and it just becomes an absolute nightmare to understand what's going.
Utilise unit testing for your controllers to ensure that dependencies are kept under control and that your code is testable.
Don't get drawn into letting jQuery and fancy clientscript define the behaviour of your application, try and use it as sparingly as you can and let it enhance your application instead.
Use partial views and HTML helpers whenever possible to ensure that your Views do not become unwieldy and a maintenance nightmare.
Use a ViewModel whenever possible.
Use a dependency injection framework to handle your dependencies (MvcContrib has several controller factories, though it's simple enough to roll your own).
Use a different controller for every section of your site (e.g., Home, Account)
Learn how to use ViewData and TempData
Learn what's the use of RenderPartial

Resources