How to Store large data in MVC4 - asp.net-mvc

I am new to MVC.I am having some doubts so please clarify me.
How to store large data in MVC4 and how to pass that data across the pages.
How to maintain user details across the pages. In webforms we are having sessions but in mvc4 how we will do.
if we are having two actionresult of same name one will be fired on POSt action. How the CLR identifies which Action method to be called..means how it will identify that POSt method is called.

Define "large data" - remember that the web is stateless, persistence is done using a database or a server-side cache. I need more information about what you want to accomplish here.
ASP.NET MVC still supports Sessions. You can access the Session collection from any Controller action.
The Post action method must have a different method signature. The usual approach is to specify the view's model as a parameter, or a FormValueCollection, for example:
-
// GET
public ActionResult Foo() {
}
// POST
[HttpPost]
public ActionResult Foo(FooModel model) {
}
OR:
// POST
[HttpPost]
public ActionResult Foo(FormValueCollection postValues) {
}

Related

MVC - Routing entry based on url

I am new to mvc and I need to create an application to where the user will be using one of 2 urls. One url will direct to a generic registration and the other will direct to a customized user profile update page. (In a website this would be in the page_load method.) Where would be the correct place to write this logic within the mvc application?
In your WhateverController, you have to create 2 methods :
public ActionResult GenericRegistration()
and
public ActionResult ProfilPage(int profilID)
So you have 2 urls : /Whatever/GenericRegistration and Whatever/ProfilPage/{id}
Additionally, you can create another ProfilPage method who can accept the POST action from your html form when the user update its data :
[HttpPost]
public ActionResult ProfilPage(MyFormModel formModel)
{
// update user profile
}

Process get and post

I have an question about the get and post process in ASP.NET MVC 4.
I'm sure that often talk about but it isn't easy to search for this topic.
Let me try to explain:
I start my controller with the standard method:
[HttpGet]
public ActionResult Item()
So, in this function I retrieve a lot of important data as example the user id and so on.
In my case, I even collect data in my viewbag() to decide if a form has to be displayed or not.
Now, if I start a post back:
[HttpPost]
public ActionResult Item(FormCollection formCollection)
the function gives as standard View() back.
The Problem is now, that after the post method, the business logic (retrieve user id and so on) of the GET method isn't called... I have tried to solve it with
return this.RedirectToAction("Item");
but is that really the solution for repeat the logic out of the start (get)? And how can I give the new values from the post method to the get method?
Best regards,
Patrik
That pattern is called Post/Redirect/Get.
To pass additional data to GET method you can use TempData and ModelStateToTempDataAttribute from MvcContrib - it passing ModelState to tempdata if Redirect is returned and tempdata to modelstate if View is returned.
[HttpGet]
[ModelStateToTempData]
public ActionResult Item(int id)
{
// prepare view
return View();
}
[HttpPost]
[ModelStateToTempData]
public ActionResult Item(FormCollection formCollection)
{
// do some business logic
int id = service.DoBusinessLogicAndReturnSomeId();
return this.RedirectToAction("Item", new { id });
}
You should avoid to have business logic in GET. All business logic should be inside POST method and after you invoke that you can redirect to GET where you prepare your view.

ASP.NET MVC security: how to check if a controller method is allowed to execute under current user's perrmissions

Given an ASP.NET MVC Controller class declaration:
public class ItemController : Controller
{
public ActionResult Index()
{
// ...
}
public ActionResult Details()
{
// ...
}
[Authorize(Roles="Admin, Editor")]
public ActionResult Edit()
{
// ...
}
[Authorize(Roles="Admin")]
public ActionResult Delete()
{
// ..
}
}
I need to reflect a list of methods in this class which may be invoked with the current user's permissions.
Please share some ideas of what could be done in this case.
Well for the new question think something along the lines of:
new ReflectedControllerDescriptor(typeof(ItemController)).GetCanonicalActions()
could be used to return the list of all available actions. I don't have ASP.NET MVC available to me at work, so I can't really check to see if the ActionDescriptor's returned by that will contain some parameter which says which members are allowed to execute them.
http://msdn.microsoft.com/en-us/library/system.web.mvc.actiondescriptor_members%28v=VS.90%29.aspx
That is the members of the ActionDescriptor, you might be able to find something in there. I'll see tonight if I can figure it out, this has gotten me kind of intrigued.
There's no universal user login/authentication system for all applications, thus this really isn't possible to create a 'universal solution'. You could create your own user login and authorization classes which you then add your own annotations to methods to do, but its going to have the same restrictions that the asp.net mvc system has, its only for your login/authorization system (or whoever extends that system).

ASP.NET MVC, JSON post to controller action with FormCollection parameter

I have a bunch of controller actions mostly used for saving data to backend storage. For now most of them use a signature like this:
//
// POST: /WidgetZone/Create
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
as you can see, it accepts FormCollection. This works fine with classic user views. Now I want to JSON- enable these actions. And I do it using JsonPox action filter like this:
//
// POST: /WidgetZone/Create
[JsonPox]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(FormCollection collection)
Will this work when the action expects FormCollection?
For instance this work without issues (of course I construct Json object in my JavaScript client-side to pass it into this action):
//
// POST: /WidgetZone/Create
[JsonPox]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(string id, string description)
It is all about a task of converting postback UI into asynchronous one, so that saves and updates would be done async. Am I on the right track? I do think that developing separate Json, XML or classic ViewResult actions is not the best way to go.
Help appreciated
This filter is based on the the OnActionExecuted method which is run after the action method is executed in order to JSON or XML serialize the returned model. What you have as input to your action method is not important. Once the action finishes execution the filter will look for the model that you stored in the ViewResult and serialize it according to the Content-Type header that's passed in the request.

ASP.NET MVC Account Controller usage guidelines?

I'm looking at the MVC account controller, and it seems to be from ASP.NET webforms. Is there any good background information on how to use it?
Can you map it to a user database table or is it better to roll your own user management?
How do you make use of it in MVC to restrict what pages a logged in user can view? Do you have to roll all of that on your own?
What resources on the web can help with understanding the ASP.NET Membership?
I'm looking at the MVC account
controller.... it seems to be from
asp.net?
Scott Guthrie explains this quite well in his blog entry about ASP.NET MVC Preview 4. He basically says that the Account Controller from the MVC sample uses the ASP.NET membership provider, so you can use any of those. (I think you can find out more about ASP.NET membership providers on the internet.) If you do not want to implement/use one of those, modifying the application to use your own user management would probably be the best option.
How do you make use of it in MVC to
restrict what pages a logged in user
can view? Do you have to roll all of
that on your own?
You can add the Authorize attribute to the controller class or action method. (Same source as above.)
// Only logged in users can access this controller.
[Authorize]
public class SomeController : Controller
{
#region Not really important for this example. :]
// Maybe rather use a BLL service here instead of the repository from the DAL, but this example is already more verbose than required.
private IStuffRepository stuffRepository;
public SomeController(IStuffRepository stuffRepository)
{
if (null == stuffRepository)
{
throw new ArgumentNullException("stuffRepository");
}
this.stuffRepository = stuffRepository;
}
#endregion
// The authorize attribute is inherited - only logged in users can use the index action.
public ActionResult Index()
{
return View();
}
// Moderators can flag stuff.
[Authorize(Roles="Moderator")]
public ActionResult Flag(int id)
{
this.stuffRepository.Flag(id);
return RedirectToAction("Index");
}
// Admins ans SysOps can delete stuff.
[Authorize(Roles="Admin,SysOp")]
public ActionResult Delete(int id)
{
this.stuffRepository.Delete(id);
return RedirectToAction("Index");
}
// Only joed can change the objects stuff. ;)
// (This is probably bullshit, of course, but I could not make any better example. I blame the fact it is late at night. :))
[Authorize(Users="COMPANY\\joed")]
public ActionResult ChangeId(int oldId, int newId)
{
this.stuffRepository.ChangeId(oldId, newId);
return RedirectToAction("Index");
}
}

Resources