Problem with IEnumerable in View - asp.net-mvc

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

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

MVC search box validation and multiple models in a view

To make my question easier to understand I attached a screenshot, containing views, model and controller scripts. Please click the link below to view the screenshot.
http://ithighway.co.uk/others/ProblemMVC.gif
Thanks!
Note:To keep things simpler I removed some lines and comments from controller and views.
In your Action method, you are not getting SearchBox model. First you need to get the model in the Action method and then check for Model.IsValid, if that condition fails simply return the model to view. That will make sure it will display the errors.
Having said that, this is server side validation. You can also do a client side validation using JQuery.
It looks like you forgot to put the #model MvcBlog.Models.User.SeachBox declaration at the top of your _SeachBox.cshtml. (which is why you have the red line under model.txtSearch)
The partial view needs this declaration to know what type of model it has.
I would also use a Html.TextBoxFor instead of a Html.TextBox for the actual search field. This will strongly bind your ViewModel to your view.

getting dropdown's text in Post action method

I have a Razor view in my asp.net MVC3 application with a dropdownlist like this:
#Html.DropDownListFor(model => model.Account.AccountType, new SelectList(Model.AccountTypes, "AccountTypeCode", "Abbreviation"))
This dropdown is inside a form. When form is posted to action method and viewmodel is filled because of model binding, It get the value(AccountTypeCode) and not the text "Abbreviation" property of dropdownlist. I want to get both of these. how can I get these in post action method.
Please suggest.
If you need more than one property of an object as a value for a dropdown, the easiest way is to create a combination of these in a partial class
EF 4, how to add partial classes this quesion should help you. You will be able to combine values under one property that you will provide to your dropdown helper.
If you don't want to use partial classes I would advise creating your own helper, that will be a lot easier than trying to use something that does not fit your needs. You can do something like :
#helper CustomDropdown(string name, IEnumerable<AccountTypes> valueList)
{
<select>
#foreach (var item in valueList)
{
<option value="#item.Abbreviation #AccountTypeCode">#item.Abbreviation</option>
}
</select>
}
Google "Creating an Inline HTML Helper" to get some valuable resources on that topic
I struggled with this recently, and the best I could do was:
ViewBag.Message = myModel.myProperty.ToString().
in the controller action. Assuming myProperty is AccountType.
Then in my view I just did
#ViewBag.Message
The next problem I encountered is that it spit out the exact text, without spacing. I had to use a helper to add spacing (but it was based on each word being capitalized, so "ThisIsSomeText" would show up as "This Is Some Text".

EditorTemplates/Object.cshtml using EditorFor() instead of Editor()?

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

Resources