Using almost same view for editing and viewing - asp.net-mvc-3

So my question is about handling both editing and viewing document data in same view. Basically, I have a view that can view document data. It has necessary fields and can show text information for user. How can I, guys, edit this document using this one view?
So seems like I need to pass some flag that indicates "editing" or "viewing" mode and show edit textboxes or just text.
Any techniques, methods to achieve this? Could you, guys, advice me something? Any blog posts, articles, manuals will be very appreciated.
Thanks in advance guys!
P.S.: it is ASP .NET MVC3 application (with Razor view engine).

I usually accomplish this using two differant actions (details and edit). You would decorate your actions using AcceptVerbs. POST vs. GET in this case. You can create a single 'partial view' that is returned by both actions. Its behavior (readonly / editable) will be dictated by the action that returns the view.
There is a very straight forward tutorial covering just this here:
CRUD
Reusing Views (Partial View)

Related

What is the right combination to use Paging and styling in MVC?

I may not be finding the best way to ask this, but I'm learning MVC by trying to build a small website. This is supposed to be a shopping website. Here I am on the part of displaying items on the page. I came through webgrid, but its displaying items in tabular form. Should I learn the tricks of tables like those in html? Because I have heard they can be modified to give any view?
Or can there be other alternatives to webgrid? I really like its Paging feature. I don't think I'm good enough right now to code for that. I have tried some googling and found jqgrid but that also seems to be good for tabular representation of data. I have been trying with this webgrid thing from last night and its really not coming together all good. What else can be the option? I mean something abstract enough to provide me good looks of a shopping website's product page, when I can provide a List of products(the model) from controller to the view? Then what?
Update: Basically I wanna know what part am I missing? Modifying Table to create good views? using webgrid properly? Or am I using the webgrid wrongly? something else should be used in this scene?
You can take a look at this simple of example of how to use Twitter Bootsrap to style a WebGrid: WebMatrix WebGrid – Re-Working the Pager.
You can also check out the Twitter Bootstrap site for examples of other ways this can be used to transform tabular data.

How to Handle Mutiple Model Bound Forms

I am buiding a UI screen for editing the details of an Ecommerce Order. The model for my view (OrderModel) has everything I need (in properties that are also ViewModels), but the UI isn't designed to be able to edit all of it at once.
For example, one part of the UI is for customer data..another for order details, and another for tracking information, each having their own "Save" buttons.
I realize that I could use one giant form and use hidden form fields to populate the non-editable fields, making each "Save" button post all the data, but that smells bad.
I'd like to segment the editable chunks into smaller ViewModels that are posted and validated individually while retaining the strong typing but I'm unsure of how to achieve this in MVC3. Will I need partial views that are called from the primary view?
FYI, I'm using ASP.NET MVC 3 with Razor syntax and client side FluentValidation.
Partial Views are a good solution. You can pass different ViewModels to each partial view. But if only sections of the overall view are updated at a time I would not do a post back on the whole page. Instead I would use Ajax calls using JQuery/Javascript to update the individual information back to the controller. I would also look into something like Knockout.js to handle the data binding on the page.

Getting a value from an MVC 3 Razor RadioButton list

I'm coming from a WebForms environment. When I wanted values from a radiobutton, the form simply got ajax reposted or I got it thru javascript.
I've searched the web, but can't seem to find an equivalent for MVC 3 Razor.
I'm not posting the form, so I can't use the FormCollection object in the ActionResult.
I simply want to get the value of which option in the RadioButton is checked when a user checks one of them.
I assume I would do this thru an Action method in a Controller once a user checks one of the values.
Note also, that this radiobutton list is in a partial view and is available to the entire website (because it is in my _Layout.cshtml view).
Can someone please help me out with what I think should be a simple task?
You need to use javascript. See this post. MVC does not have the concept of an Auto-Postback like in Webforms. This is an optimization in the page loading time (no self-managed viewstate). This means you need to manage the statefulness of controls yourself.
In short, if you're not posting (or "getting") the form then the value won't be available in the controller...more specifically, neither the controller nor it's actions will be called at all.
If you're trying to do something strictly UI related, then you may want to consider jQuery.
If you're trying to do some sort of model validation inside of the view, you may want to reconsider your approach and usage of the MVC pattern.

Updating data with information received from the view (Grails)

I am writing a Grails app and I have a quick question about the best way to do something. My controller has 2 lists of data, both of which are displayed on the view in separate HTML tables. What I want to do is to have a button that will allow you to move an item from one list to the other. What is the best way to do this?
I have done some research and using some form of AJAX seems to be the general consensus on the best way to do this. Is there any other way? The heart of the problem is that I can't figure out how to update the data contained in the contoller based on the button the user presses in the view.
Thanks
I solved this by just updating the model to reflect the new lists and rerendering the page

Drawing a part of a page only after another part was "submitted"

ASP.NET MVC noob here
I was writing a quiz application where a user can select her preferences (e.g. difficulty, number of question etc.), and once she hit a submit button - she got a new page with questions.
The preferences are represented as a "Preferences" object, and the questions are IEnumerable of Question.
This all worked well.
Now I decide both parts should be in the same page - and I don't know how to accomplish that:
Should I have a new model class that is a composition of these two parts?
And also - How will I make the "questions" part appear only after the user completed filling up her preferences and clicked a button?
Should I use AJAX?
I also read a little about partial views and RenderSection.. But I really couldn't understand which approach is the most appropriate for my scenario.
So how should I draw two parts of a page, where the second is only displayed after the first is submitted?
Thanks.
How familiar are you with AJAX? If I had to guess I would think a good way to do what you want to do is to have an AJAX call which is linked to an action when the user submits their preferences. The action can then return a partial view which you can have appear on the page without a reload via AJAX.

Resources