I have a View that contains a usercontrol. The usercontrol is rendered using:
<% Html.RenderPartial("GeneralStuff", Model.General, ViewData); %>
My problem is that the usercontrol renders nicely with values from the model but when I post values edited in the usercontrol they are not mapped back to Model.General. I know I can find the values in Request.Form but I really thought that MVC would manage to map these values back to the model.
My usercontrol:
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<namespace.Models.GeneralViewModel>" %>
<fieldset>
<div>
<%= Html.LabelFor(model => model.Value)%>
<%= Html.TextBoxFor(model => model.Value)%>
</div>
</fieldset>
I'm using .Net MVC 2
Thanks for any help!
Problem solved!
My usercontrol is rendered with:
<%= Html.EditorFor(model => model.General); %>
My model looks like this:
public class TestEditViewModel
{
[UIHint("GeneralViewModel")]
public GeneralViewModel General { get; set; }
}
And my usercontrol is placed under Views->Shared->EditorTemplates and named "GeneralViewModel.ascx"
Related
I have an ASP.NET MVC2 application. On one of my pages I have a textbox defined in the following manner:
<%: Html.TextBoxFor(model => model.PostCode) %>
Which is working perfectly.
However, for specific countries (model.Country) I do not want to show this TextBox.
What is the best way of implementing this?
This is an .aspx file, not .cshtml
Thanks
Create a property in your model class, and try this:
#if (!Model.IsSpecificCountry) {
#Html.TextBoxFor(model => model.PostCode)
}
Update:
<%if (!Model.IsSpecificCountry) { %>
<%= Html.TextBoxFor(model => model.PostCode) %>
<% } %>
Essentially what I have is an entity class (Article) and one article may belong to n categories. This is where one would expect a field for the categories (say an ICollection) but the database is not built that way and I cannot modify the Entity Framework model.
When a category is selected in the view, the user has to select a property (a string list between 1 to 10. It will look like this:
Category Option
===============================
[x] Category 1 |---------|v|
[x] Category 2 |---------|v|
[x] Category 3 |---------|v|
[x] Category 4 |---------|v|
[x] - checkbox
|---|v| - dropdown list
However I'm thinking that perhaps I could create a ViewModel for this particular case. I was thinking something like
namespace Models.ViewModels
{
public class ArticleViewModel
{
public Article Article { get; set; }
public ICollection<Category> Categories { get; set; }
public ArticleViewModel()
{
Categories = new List<Category>();
}
}
}
However when I post the form to the controller the ArticleViewModel and all of its internals are null. I'm not sure why.
The view itself is pretty basic, it will have two separate forms: one for modifying article details and one for assigning categories to the article.
As a guy new to asp.net mvc, I find it a little difficult to wrap my head around this problem.
Which approach would you recommend?
Update: added code for the view.
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Models.ViewModels.ArticleViewModel>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Modify article: <%= Model.Article.Title %>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% using (Html.BeginForm()) {%>
<%= Html.ValidationSummary(true) %>
<fieldset>
<legend>Properties</legend>
<div class="property">
<span>
<%= Html.LabelFor(model => Model.Article.Title) %>
<%= Html.TextBoxFor(model => Model.Article.Title) %>
<%= Html.ValidationMessageFor(model => Model.Article.Title) %>
</span>
</div>
<div class="property">
<span>
<%= Html.LabelFor(model => Model.Article.Type) %>
<%= Html.TextBoxFor(model => Model.Article.Type) %>
<%= Html.ValidationMessageFor(model => Model.Article.Type) %>
</span>
</div>
[continued...]
<div>
<%= Html.HiddenFor(model => Model.Article.Id) %>
<input type="submit" value="Save" />
</div>
</fieldset>
<% } %>
</asp:Content>
Routing looks like this:
routes.MapRoute(
"Articles",
"Articles/{action}/{id}",
new { controller = "Articles", action = "Edit" }
);
The whole point of MVC is that your View is decoupled from your Model. It doesn't matter what your EF entities look like.
Create your view and view model classes the way you want. Don't think about your DB structure. If you need a collection of Categories than create it. This code should represent the 'domain' of UI.
In the controller map your view model to EF and vice versa. It's controllers responsibility to understand data access code (or service layer code) and view code.
This approach allows you to take a full advantage of MVC and decoupling between UI and database. Although you end up writing a bit more code for your models it pays up with interest later on (you can modify your db without touching UI and vice versa).
If you find yourself spending a lot of time copying data from EF entities to models than you might want to take a look at this brilliant library: AutoMapper
When you set up your view like that, each of those fields is going to have the same name attribute and I don't think MVC will know how to map it back to a single item properly. What does your [HttpPost] action look like? If its anything like:
[HttpPost]
public ActionResult Edit( ArticleViewModel model )
{
..
}
then I doubt it will map correctly. You could maybe try accepting List<ArticleViewModel> but I don't think that would work correctly without a CustomModelBinder.
What #Jakub said is correct: de-couple your thinking and design your ViewModel in the most logical way for the view; you can worry about mapping it back to the service layer later. Its a paradigm shift for sure, but one that's worth making. = )
The ViewUserControl below results in the following error at runtime:
The Collection template was used with an object of type 'System.Data.Entity.DynamicProxies.Collection_1D9779ACB92AE24E3428C288EA7B1480A6477CF8861FB7582692E775613EFB3A', which does not implement System.IEnumerable.
The error occures on this line: <%: Html.EditorFor(model => model) %>
If I change the name of the model object to Collection2 it works. Does it gets confused because Collection is also the name of an object in the .net framework?
<%# Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<CollectionManager.Models.Collection>" %>
<% Html.EnableClientValidation(); %>
<% using (Html.BeginForm()) { %>
<%: Html.ValidationSummary(true) %>
<%: Html.EditorFor(model => model) %>
<input type="submit" value="Save" />
<% } %>
remco - yes, i think that would definately be a reserved word. not sure about it getting confused but definately you should take care with naming when potential clashes could occur. the same thing is true of variable names, tho you can prefix them with # to override that i.e. string #absract would be allowable, whereas string abstract wouldn't.
go with the 'reserved' flow :)
jim
I have a for to update a list of entities it is possible to do something like these
<%using (Html.BeginForm())
{
%>
<% foreach (var entity in Model)
{
%>
<p>
<%= Html.TextBox("Entity.Name", entity.Name) %>
</p>
<% } %>
<input type="submit" value="Update" />
<% } %>
and then in the action receive a list of entities? or a list of names...
I don't want to create a form for each entity with his own button, i want to update all the entities toghether.
Wich options i have to do these?
Thanks in advance,
Alfredo
<%= Html.TextBox("name", entity.Name) %>
public ActionResult foo(string[] name)
or
<%= Html.TextBox("Entity["+index+"].Name", entity.Name) %>
//will create list of entities from form values
public ActionResult foo(IList<Entity> entity)
Process in Asp.Net Mvc terminology is called model binding.
This article might be worth checking out. And another one about binding lists.
I am trying to build a generic form submission system using ASP.NET MVC. I'd like to make it as easy as possible to create forms with a form view and a "success" view. Using the WebForms method, this was easy and could be accomplished with templates or multiviews. With MVC, I'm a bit stuck.
Here's what I'd like to emulate:
<% if (formNotSubmitted) { %>
<% Html.BeginForm(...); %>
<%= Html.TextBox("FirstName") %>
<%= Html.TextBox("LastName") %>
<input id='submit' type='submit' value='Submit' />
<%= Html.ValidationSummary %>
<% Html.EndForm(); %>
<% } else { %>
<p>Thank you!</p>
<p><img src='thanks.jpg' /></p>
<p>Other items here maybe.</p>
<% } %>
Ideally, I'd like to use Ajax, but also have it work with a straight POST. I'd also like to wrap this somehow to avoid the "if..else" code. What makes this harder than a typical ASP.NET MVC Ajax form is that the controller won't know what the success message/content is supposed to be. Most demos I've seen have the controller send back a message, but I need that code in the view.
Any guidance is appreciated.
You could it with the Controller
[AcceptVerbs("GET")]
public ActionResult Signup( )
{
// somecode that builds an html string
ViewData["form"] = htmlStringYouBuilt;
}
[AcceptVerbs("POST")]
public ActionResult Login( string username, string password )
{
// etc
}
then in the view
<%= ViewData["form"] %>
I've been able to use this technique successfully.