Post back complex object from client side - asp.net-mvc

I'm using ASP.NET MVC and Entity Framework. I'm going to pass a complex entity to the client side and allow the user to modify it, and post it back to the controller. But I don't know how to do that and whether the client side entity will lost relationship tracking of EF as it is detached from object context.

ASP.NET MVC is capable of Model Binding complex objects and it's quite good at it. An easy way to do this is to name your view fields that same as the properties for your object. That way in your action method that the form posts to you only need the complex object as a parameter. Example:
<% using(Html.BeginForm()) { %>
<%= Html.TextBox("Property1") %>
<%= Html.TextBox("Property2") %>
<%= Html.TextBox("Property3") %>
<%= Html.TextBox("Property4") %>
<%= Html.CheckBox("PropertyBool") %>
<%= Html.TextBox("Property5") %>
<% } %>
Which posts to an action method like so:
public ActionResult Index(ComplexObject complexoObject)
{
}
That's a fairly simple example as you could have different form controls in the view for corresponding properties of the object. If your object is very complex you could always write your own model binder for it and over-ride the default model binder.

The entity will be detached. You will need to fetch the entity again, update the properties, and then commit the changes. Alternatively you could reattach the entity, but this can be tricky when you are dealing with multiple related entities being attached at the same time. If this is the case for you, check out http://www.codeproject.com/KB/architecture/attachobjectgraph.aspx.

You are saying that you display a list of 100 questions on one single page? I hope you are not doing that. First of all, this is not very user friendly. It involves a lot of scrolling down, user can get lost, its error prone and if something goes wrong he has to do it all over again.
A better approach is one or few questions at a time. With Next/Prev buttons for moving between them.
You could make it even easier for yourself and forget about postbacks and go the AJAX way - issue a post request to some Json service the AJAX way. And save the progress after each question, so if something goes wrong user can resume later from where he left off. It will be much more maintable, faster and user friendlier.

Related

Can a controller influence the _layout.cshtml file?

I'm stuck! I'm under the impression that the _layout.cshtml file is used for MasterPage-like content. Everything there is rendered on every page. Naturally, I want to write the code for rendering my sidebar menu in that file.
I want to dynamically display a list of Categories from my DB, but I'm having a problem with passing the actual model of categories to Layout.cshtml since it seems no controller actually touches it.
Any suggestions?
Otherwise please tell me how to approach this problem. I've been wracking my brain for the past three days and still no elegant solution.
I need to:
Dynamically fetch a list of Categories from the DB.
Display this list of Categories on every single view. (Hence the use of _layout.cshtml)
Elegantly handle each different categories click.
I'm at my wits end. :P How would you solve this?
_layout.cshtml
#if(isSectionDefined("Categories"))
{
<div id="sidebar">
#RenderSection("Categories", required: false )
</div>
}
index.cshtml
#section Categories {
<ul>
<li>Category One</li>
<li>Category Two</li>
<li>Category Three</li>
</ul>
}
see this : http://weblogs.asp.net/scottgu/archive/2010/12/30/asp-net-mvc-3-layouts-and-sections-with-razor.aspx
Any viewmodel that you pass to your view is automatically available within your master page. If you do not use RenderAction/Action which is the best approach, then you must create the necessary master page data in every action and add it to viewdata - either by having a common base class for your strongly typed viewmodel that contains all master page data or by using the viewdata dictionary.
I would strongly recommend that you go down the html.action approach though. In this way, you have a totally separate controller action for dealing with your list of categories. This action can retrieve the neccesary category data and return the categorylist usercontrol as a partialview and you will not have to worry about polluting all your other actions with this data.
As I see it, ViewData (and its relatives like ViewBag, Model, etc.) is meant for the specific current view. Your _Layout.cshtml is not specific to the current view; and it would be awkward if EVERY controller would have to pass the categories data in addition to whatever else data it needs to pass for the view.
Instead, what I do, is provide a static method in one of my helper classes that retrieves the categories from the DB. I also do some caching there, so that I do not have to hit the DB on every single request. The _Layout.cshtml then simply calls this static method. Simple and elegant.
If you wish, you can bring this out to a partial view, make it a helper method, whatever.
One note of caution though - my custom error view also uses the same _Layout.cshtml, and if the DB goes down, you get an exception trying to display the exception. ASP.NET MVC is smart enough to detect this and abort processing, but you're left with a nondescript default error page. What I did was to place try...catch statements around these dangerous calls, which quietly ignore the exception if the current page is the error view.
I've achieved something similar by having my ViewModels implement an Interface which has members that contain the menu data. In my action method I set that data. Then in my view I check to see if my view-model implements that inteface, pull the menu data out and render the menu (in a partial view actually)

ASP.NET MVC Catching 'Save' POST with RenderPartial

I have a asp.net mvc page which renders a record from a database, it uses RenderPartial to call another view which renders an editable list of items related to that record.
My problem is I want a since save / submit button which not only saves changes made for that record but also changes made in the RenderPartial part... I have created a method accepting POST in the RenderPartials controller but it doesn't get called? Any ideas? Or am I using RenderPartial wrongly? I did it this way so that I have a controller that handles the subset of data
Update:
I don't think I've been clear enough:
Imagine a situation where you have a page that is filled with information from lots of different tables in a database... for example imagine you have a record of a person, and then you have all the links they have to Organisations that you want to list on the page, so the page contains:
Individual Name, Email, etc...
AND
Organisation Link 1
Organisation Link 2, etc... from a link table
Because of the amount of data I want to render of the page, I figured using different controllers to render each part would make sense.. but then when saving the data do I have to use just one controller method or can I call one controller to another... I only have one form and one 'save' button for the whole page
I hope this is clearer?
You don't need to have a specific controller for each controller, although I suspect you meant to say "I have created a method accepting POST in the RenderPartial's action ..."?
When you accept the default html helper commands, it can sometimes get confusing which action will be called. For a particular web page, the tag will determine where to POST values. So the action called will be dependent on the controller/action you specify in your Html.BeginForm call.
For example, here's a form we use from an ascx page:
<% using (Html.BeginForm("InvoiceDetail" //Action
, "CompanyInvoice" //Controller
, FormMethod.Post
, new { #id = "InvoiceForm", #autocomplete = "off" }))
{%>
A form can post to any action, in any controller. Not that you'd want to, but it's quite flexible. You can also have multiple forms in a single web page now, which is great for complex forms.

How to pass MVC User Controls Different Data

I have a user control I use multiple times on a page. It uniformly formats some object "X" and allows certain interactions. What I'd like to do is to pass a separate object to each separate instance of the user control. I'm currently rendering the controls as below, and just depending on some specific ViewData being set. I'd like to make it more generic than that.
<% Html.RenderPartial("Vignet"); %>
Use a strongly typed partial view, and pass the model explicitly:
<% Html.RenderPartial("Vignet", Model.VignetData); %>

View Models (ViewData), UserControls/Partials and Global variables - best practice?

I'm trying to figure out a good way to have 'global' members (such as CurrentUser, Theme etc.) in all of my partials as well as in my views.
I don't want to have a logic class that can return this data (like BL.CurrentUser) I do think it needs to be a part of the Model in my views So I tried inheriting from BaseViewData with these members. In my controllers, in this way or another (a filter or base method in my BaseController), I create an instance of the inheriting class and pass it as a view data. Everything's perfect till this point, cause then I have my view data available on the main View with the base members. But what about partials?
If I have a simple partial that needs to display a blog post then it looks like this:
<%# Control Language="C#" AutoEventWireup="true" Inherits="ViewUserControl<Post>" %>
and simple code to render this partial in my view (that its model.Posts is IEnumerable<Post>):
<%foreach (Post p in this.Model.Posts) {%>
<%Html.RenderPartial("Post",p); %>
<%}%>
Since the partial's Model isn't BaseViewData, I don't have access to those properties. Hence, I tried to make a class named PostViewData which inherits from BaseViewData, but then my containing views will have a code to actually create the PostViewData in them in order to pass it to the partial:
<%Html.RenderPartial("Post",new PostViewData { Post=p,CurrentUser=Model.CurrentUser,... }); %>
Or I could use a copy constructor
<%Html.RenderPartial("Post",new PostViewData(Model) { Post=p }); %>
I just wonder if there's any other way to implement this before I move on.
Any suggestions?
Thanks!
Have you considered keeping these things in the session and writing a strongly-typed wrapper around the session that will give you access to this information? Then in any view you can simply create a new wrapper class with the ViewPage's (or ViewUserControl's) Session property and use it.

How can I make a user control's form portable to other pages

I have a user control that will be used in several pages (as is the custom with user controls) that contains a form.
I was thinking on how to guarantee its functionality.
First I wanted to have a single controller with a single method accept the values for this form. The only thing blocking me there is that after this method is done with the calculations etc it has to forward to an other controller and method depending on the view the user control originated on.
Then I wanted all the controllers that have this usercontrol to have the same fixed method that would forward then to the default page for that controller. This would only work if the fixed method had exactly one option to forward to (this is the case now, but maybe not tomorow) AND that I could somehow get the calling controller when I build my form tag.
Is there any clean way to either get one of my suggestions working or is there maybe an other option to get this thing going?
You know, you can add public properties to your user control and then, in the markup, refer to those properties to do the appropriate things in the current context...
For example:
MyControl.ascx.cs:
/*snip*/
public string FormAction {get;set;}
/*snip*/
MyControl.ascx:
<% using(BeginForm(FormAction)){ %>
<!-- yadda -->
<% } %>
And in any aspx:
<!-- boring html goes here -->
<%=Html.RenderUserControl(“~/Views/Shared/MyControl.ascx”,null /*orwhatever*/, new {FormAction="ActionThatHandlesMyFormPostLol"})%>
BeginForm takes a number of different arguments that will allow you to control where and how your form posts. I'm not 100% clear on your issue, but I hope this gives you an idea on how to move forward on it....

Resources