asp.net mvc Controller - View logic - asp.net-mvc

I'm trying to learn the asp.net mvc. I have worked with most of the samples that MS have published.
Most of the samples is just about CRUD.
I'm pretty good at working with webforms, but now I kind of miss the old easy world.
But, my question is: I have a detail page that is connected to an order, I have the order details comming along all well. I got my order detail lines kind of work. But now I would like to do different things with this order, like add more order lines, change the order status with a button, email the order and so on. In webforms I just added eventhandlers on the click event, but here... Do I need multiple forms? How do I make serverside code for example when someone wants to change the orderstatus with a click of a button?

Hi i can recomend you Pro ASP.NET MVC 2 Framework book by Steven Sanderson

I'd recommend you go through the entire NerdDinner tutorial.
But to answer your question:
There really are no forms in asp.net MVC. There are views, which show your order, and there are controllers/actions which allow you to run code to generate views, update your database, etc. You ask if you need another form - the answer is No. You will, however, need at least a new action on a controller, and possibly a new view to confirm that the order was changed. An overview to a very simplified solution would be:
You want to allow an order to be marked as closed by clicking a button. Assume you have a controller called Order, and a view (in the order views folder) called Details. You'll have to add an action which takes an integer as a parameter to your order controller called "Close". In that action, you'll read the integer parameter (the order ID) and execute code to update that particular order to closed. You'll probably want to return the same view with the udpated status from this action. You'll have to add a link (probably using HTML.ActionLink function) to your view to call this new action.

In MVC everything is Post and Get actions. You can post to difference controllers. It seems you need to spent a bit more time reading about it. If you do not fancy reading - try some nice video tutorials for beginners. http://www.asp.net/mvc

Related

How to create a big view (page) in MVC?

I am new to MVC and learning some stuff of views, partial views etc.
I would like to make a simple page where lets consider "Login" and "Register" both comes together.
Like facebook.
I have found all the examples which is all having one view on page. I guess, to make a big view (complete page) we need to have partical views to use in one big view.
unfortunately, i am not able to find perfect example, may be lake of searching power or exact term to use for MVC.
I want to make a page where many partial views use in one big view and create a big page.
Can any one please help me to get video link or help link which can be useful and how exactly it works?
Check out the partial views in
http://www.pluralsight-training.net/microsoft/players/PSODPlayer?author=scott-allen&name=mvc3-building-views&mode=live&clip=0&course=aspdotnet-mvc3-intro
Using #Html.Partial is fairly easy, I generally prefer #Html.Action to render another controllers action method in line, and it makes more sense for me in a situation like you mentioned above.
Something else to consider are the ajax type dialogs for login, now in the mvc4 developer preview templates at: http://www.asp.net/mvc/mvc4

How do I make an MVC Billing page?

I've made a music store using the tutorials on ASP.net and they do not show you how to make a billing page. What I would like to know is would I have to make a new controller and view for it or will I just need a view and to add a piece of code to an existing controller? Also, how can I make the page so that it authorizes the details given, for example, will know when the card details are not correct like the wrong security code or wrong card number in general?
Much appreciated
So you need to continue to tutorials on asp.net mvc site you should try the tutorial on models page code first with entity framework there are so many samples about validation.
For the credicard validation there is an algorythym of this named Luhn Formula, so really you don't have to know how to validate, there is a asp.net code that has been written and works on the link also you can use javascript validators on view to give better service.
http://www.codeproject.com/KB/aspnet/wdxcreditcardvalidation.aspx
you just need to write a code on controller (it just to show you how it's done, you may need to write additional codes)
if(luhnisvalid(Order.billingdetails.CreditCard))
{
// then adding to database
}
or return to view
return(View(Order));
and finally continue to this tutorial it will give you so much information, after the tutorial i'm sure that you can make the billing part.
http://www.asp.net/entity-framework/tutorials/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

how to access the radioButton in grid View?

using data source we can show the data in web page. but i want that there is radio button with all rows and after accessing the radio button the given functionality works?
how to do it please explain?
you need to be more clear with your question - if you already have some code then post it and then ask the how your code needs to do something specific
since the comment was amended
in ItemDataBound event
do something like (e.FindControl("controlname") as RadioButton).SelectedValue
im not quite sure if its c# or vb that you are writing in so cant be more specific at this time
Based on your comment in the other answer I understand that you're actually using WebForms server controls with Asp.net MVC application.
Avoid mixing an matching Asp.net MVC with WebForms
Unless you're skilled in both platforms I suggest you avoid this. It will only bring you headaches in the long run. Displaying and consuming checkboxes/radiobuttons (or any List<T>) should be done differently in MVC. Check this questions (and answer) out that will help you out and show you how this can be done in pure MVC way.
It also explains what things to take care when you dynamically add these items on the client side using Javascript.

MVC organization for a blog

I'm still just getting into MVC, and for my first real project I plan on creating a blog. This will be extremely basic (at least at first). Everything I need is going to be on the same page. Here are the initial features I am shooting for:
User should be able to log in, but not register (I will be the only one able to post, and I added myself directly to the DB.
Blog posts should be listed in descending order with a Title, a post date, and the body. No comments required for now.
The bottom of the page will always have an area to make a new post, assuming you're logged in.
Since I'm still new to the MVC structure, I'd like some advice on how it should be organized.
For my models, I figured I should have a post repository and a BlogPost class for the post data which can be used for both the posting and retrieving. I would also need a class for the user.
When it comes to controllers I'm a bit less confident. Should I have a different controller for every type of action? For example, posting should have a controller, retrieving should have a controller, logging in should have a controller, etc?
As for the views, since I really only need a single page, should I only have a single view and have that view output the appropriate content from my controllers?
Just let me know if I'm on the right track, I suppose. If my thought process is way off, please tell me. I've just started working my way through Steven Sanderson's MVC 2 book, but I feel like I need to go out on my own and play between my reading sessions.
Thanks.
Controllers should be grouped by functionality. You could also have a controller per resource (REST). You could have an AuthenticationController which handles authentication and PostsController which will handle the posts retrieval and adding a new post. As far as the views are concerned assuming you will have a single page that will list posts and add new posts you could have a single view but maybe with multiple partial views/editor/display templates.

What is the proper structure for Asp.net MVC?

I read the Pro .Net Asp.net MCV book over the weekend and it provides some good examples on setting it up and using it. However my question is what is the structure of an MVC project should be. I ran into problems once I started trying to transfer control from one controller to another. It seems that you can have multiple views within one controller. Also when you execute the Redirect("Action", "Controller") command it seems that the routing wants to look for the view within a sub of that controller. So my questions are:
Is there rule of thumb of 1 controller to 1 view?
Should you call another controller from a controller?
What is the proper way to transfer control from one controller to another?
You can have as many views/partial views per controller. The rule of thumb as far as one can deduce it from the MVC samples is, that a controller encapsulates a set of functionality that belongs together, e.g. listing products and creating, updating, deleting as single product.
You can use Html.ActionLink to route from one view to another. To call one controller from another, IMHO, makes only sense for partial views - however that depends on the problem.
Html.ActionLink or Html.RouteLink.

Resources