ASP.net MVC Routing problems - c#

I have two problems;
First, I can not go to the main page URL. It is not working as it is on the login page:
public ActionResult Index()
{
return View();
}
public ActionResult Login(string email, string password)
{
model = BLcontext.GetUserLogin(email, password);
if (model.UserID > 0)
{
return Redirect("/Home/Index");
or return PartialView("Index");
or return RedirectToAction("Index");
}
return PartialView("Login");
}
Second, how can I get userId value?
clientAuth "c495600a-71b3-44cb-a577-634426597c82,{\"UserID\":2,\"CurrentSessionID\":\"00000000-0000-0000-0000-000000000000\"},16.01.2015 15:50:53"
I did it
clientAuth.Split(',')[1]
but I see it.. I want value 2
return :"{\"UserID\":2"

I'm not entirely sure what or where your problem is based on what you've submitted. However, it should be as easy as this:
Home Controller: (Default Defined in your Routes)
public ActionResult Index()
{
return View();
}
By default that is a Get, which will automatically be returned when you call the raw controller.
On your other page, when you have an Event that occurs from a user interaction, you simply declare the method and direct to proper action like:
[HttpPost]
public ActionResult ValidateCustomer(int? id)
{
if(id != null)
return View("/SomeOtherController/Example");
return View("/Home/Index");
}
That would take the <form action="/Home/ValidateCustomer" method="POST"> and automatically force the submit input to pass your parameter in your fields, then go to the ActionResult ValidateCustomer hopefully this clarifies navigation a bit for you.

Related

Why does return View (user) clear the query string in ASP.NET MVC?

I use ASP.NET MVC 5.
I have an AccountController and a ChangePassword(string guid) action method with a HttpGet attribute.
This action gets a guid and shows change password view, for a specific user.
URL address in browser is for example:
https://localhost:3434/ChangePassword?guid=21725E33A1FD4FCE909770B8926FB294
When user enters a valid value, everything is OK.
But if the user enters an invalid value, I return a view (user) to show the view with entered value.
URL is changed to
https://localhost:3434/ChangePassword
and we lose the guid in the query string.
Is there any way to save the guid in the query string after post?
[HttpGet]
public ActionResult ChangePassword(string guid)
{
return View();
}
[HttpPost]
public ActionResult ChangePassword(clsUser user)
{
if (!ModelState.IsValid)
{
return View(user);
}
else
{
do something ....
}
}

ASP.NET MVC - Cannot redirect to Create action

I've had a look around at the other questions related to redirecting to views, and none seem to fix my issue. I have a intermediary step to get to my Create action, and I cannot seem to redirect from that step's view to the Create view. An approximation of my actual code is:
public ActionResult SelectDependancy()
{
ViewBag.ProductID = new SelectList(db.Products, "ID", "Name");
ViewBag.ComponentID = new SelectList(db.Components, "ID", "Name");
return View();
}
The view for this action has a POST method that results in a call to SelectDependancy(string, string).
This POST is a standard POST and not an js AJAX POST
[HttpPost]
[ValidateAntiForgeryToken]
public void SelectDependancy(string ProductID, string ComponentID)
{
FilteredCreate(ProductID, ComponentID);
}
private ActionResult FilteredCreate(string ProductID, string ComponentID)
{
//Filter values based on ProductID + ComponentID
return RedirectToAction("Create");
}
The return RedirectToAction("Create"); doesn't seem to work. I have also tried changing it to return View("Create") (changing the return type of the method as well)
You must return ActionResult from SelectDependancy in order to redirect.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SelectDependancy(string ProductID, string ComponentID)
{
return FilteredCreate(ProductID, ComponentID);
}

c# mvc - issue redirecting to action when using custom ActionName attribute

I have an action method that goes like so:
// GET: admin/cms/add-page
[HttpGet]
[ActionName("add-page")]
public ActionResult AddPage()
{
return View("AddPage");
}
// POST: admin/cms/add-page
[HttpPost]
[ActionName("add-page")]
public ActionResult AddPage(PagesVM pagesVM)
{
...
return RedirectToAction("AddPage");
}
But the redirect redirects to standard url and not the actual and different ActionName - it should redirect to mysite/admin/cms/AddPage instead of mysite/admin/cms/add-page
if you want to return to the same view and keep the form values use:
return View(pagesVM);
if you want to return the same view with empty values use:
return View(new PagesVM()):

How to return ActionResult with specific View (not the controller name)

I have a method SendMail in the MVC Controller.This method calls other method ValidateLogin. This is the signature of the Validate Login:
private ActionResult ValidateLogin(Models.ResetPassword model)
When I call the ValidateLogin from SendMail, this exception appears because the controller try to search a view SendMail, but I want to load the ResetPassword View:
Global Error - The view 'SendMail' or its master was not found or no view engine supports the searched locations. The following locations were searched: ...
This is the code of the SendMail:
public ActionResult SendMail(string login)
{
return ValidateLogin(login);
}
How Can I override the View on the return statement?
Thanks in advance
private ActionResult SendMail(string login)
{
return View("~/Views/SpecificView.cshtml")
}
You can directly point towards specifc view by pointing to their location explicitly ..
finally, this was the solution
return View("ResetPassword", new ResetPassword
{
fields= fields
});
The View method has a overload which get a string to a viewName. Sometimes you want to pass a string as a model and asp.net framework confuses it trying to find a view with the value string. Try something like this:
public ActionResult SendMail(string login)
{
this.Model = login; // set the model
return View("ValidateLogin"); // reponse the ValidateLogin view
}
If SendMail was a POST, you should use the POST-REDIRECT-GET pattern
public ActionResult SendMail(string login)
{
...
return RedirectToAction("ResetPassword", login);
}
public ActionResult ResetPassword(string login)
{
...
return View("ResetPassword", login);
}
This will protect you from a double-post in IE
You can return view by a name like this
return View("viewnamehere");

Why is this URL like this in MVC3?

I was under the impression that every View in your application has it's own unique URL. For example:
Home/Index
Home/Test
Home/Error
Home/Help
In my Upload controller I call on the Error view. Yet the URL stays on what it was before, not changing to reflect the error url.
[HttpPost]
public ActionResult Index(HttpPostedFileBase excelFile)
{
if (excelFile != null)
{
*Snip for brevity, everything is peachy here.*
return View();
}
else
{
return View("Error");
}
}
Any suggestions why this is the case?
Shouldn't the URL be /Upload/Error? Thank you for your help. :)
URLs do not map to Views.
URLs map to Controller actions.
See this http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx
If you want a URL of /Upload/Error
You could make:
public ActionResult Error()
{
return View();
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase excelFile)
{
if (excelFile != null)
{
*Snip for brevity, everything is peachy here.*
return View();
}
else
{
return RedirectToAction("Error","Upload");
}
}
You are returning the content of the View. If you want the URL to change, you need to RedirectToAction()
If you want the URL to change to /Upload/Error, here's what you would add to your UploadController:
public ActionResult Error()
{
return View();
}
Then, instead of returning the Error view, you would call: return RedirectToAction("Error","Upload");.
This basically shows the difference between controllers, actions, and views - controller actions can return any view they want (or other ActionResult) to a request, but only on one URL, unless they "reroute" the request to another action.
In ASP.NET MVC every URL maps to a controller/action. So you can return whatever view from your controller action, this doesn't change the URL.
If you want to redirect to an error Page, then either include a ErrorController in your project or an Error action in your FileUploadController and then do a Redirect to the appropriate action:
public class ErrorController : Controller
{
public ActionResult FileUploadError()
{
return View(); //returns view "FileUploadError"
}
}
public class FileUploadController : Controller // the controller you use to upload your files
{
public ActionResult Error()
{
return View(); //return view "Error"
}
public ActionResult Index(HttpPostedFileBase excelFile) // action from your post
{
//... do the upload stuff
else
{
return RedirectToAction("Error"); // if you want to use the Error action in this controller;
// or
return RedirectToAction("FileUploadError", "Error"); // if you want to use the action on the ErrorController
}
}
}

Categories