Server Error '\' in Application - asp.net

am working on my first MVC 5 application. Unfortunately I have run into an error and after searching the internet for the past 4 hours, cannot figure how to fix it. Basically I keep getting the following error.
I understand what the error means. But my Profile view is supposed to be under the Manage view. Basically when a user logs in they should see a new link that says view my profile. Its location should be /Account/Profile not /Home/Profile.
Do I need to change this in the application routing or should I remove the reference I have to this view in the HomeController and instead move it to the AccountController?
Thanks in advance for the help.

The controller that generates a view doesn't restrict you to only that controller, your links can point to any controller. So in your view when you render the link to the Profile view you should specify the controller name.
In the following example I'm using the overload of ActionLink that lets me specify the controller (i.e. Account) that contains the action (i.e. Profile).
Html.ActionLink( "View My Profile", "Profile", "Account" )

ASP.NET MVC by default checks first for the respective view in \Views\[ControllerDirectory]\, but after that, if it doesn't find a match, then it looks in \Views\Shared folder.
Having a view in the shared directory you can share views across multiple controllers. You can add your Profile view to the Shared subdirectory and you should be fine. If you still prefer to have the Profile view under Account Controller and want to reference from other controller's action then change your return statement to specify the location of the view like below.
public ActionResult ProfileAction()
{
return View("../Account/Profile");
}

Related

ASP .NET MVC - How to redirect a user control to the view its called from?

I have a user control that creates a new record in a database. After it creates the new record, I would like to redirect it back to the view its from. The purpose of this is to refresh the view so that it can show the newly created record. The problem is that user control can exists in more than one view, so how can i know which view is the user control from? so that I can achieve the above scenario? Thanks,
RWendi
I have a user control that creates a new record in a database.
No, it doesn't. It just renders HTML, and nothing else.
It's the controller action which creates a new record in the database, and thus this is where the redirect should happen.
The [HttpPost] action which accepts the model/form should perform the redirect after the save has been completed.
E.g:
public ActionResult Save(SomeModel model)
{
db.Save(model);
return RedirectToAction("Index");
}
I'm assuming that the "view" you want to refresh is the same page, regardless of which page the user controller was rendered on, therefore the above code is fine.
On a side note, you shouldn't be using user controls (e.g partials) for rendering forms.
You should be using editor templates. The presentation code which renders the form (and specifies which action to post to) should be in the view, not in the user control.
EDIT - example of how to render form on Views:
Instead of doing this on a View:
#Html.Partial("_SomeModel")
Do this:
#using (Html.BeginForm()) {
#Html.EditorFor(model => model.SomeModel)
}
And place the form markup in the editor template. The key thing here is IMO the Views should be responsible for setting up the form, not the user control.
There are several ways,
1) You can use this.Request.UrlReferrer.AbsoluteUri , which will always give you the Url of your page.
2) Or, you can have a hidden property in ... in PartialView, which will hold the value for current url. (#this.ViewContext.ParentActionViewContext.HttpContext.Request.UrlReferrer.AbsoluteUri)
But only if you are using #Html.RenderAction.
In the child action, you can redirect to either of above urls. (First approach is much more better and will work in all scenarios)
I think the refresh is responsibility of the action that renders the view your user control is from.
You can have scenarios (or views) where the inserting is done in one action and then pass the request to another action (maybe a list) that shows the newly created record.
Or you can have other scenarios where the inserting is just part of what you want to save to the db and refreshing the original view may cause the loss of data.
Maybe what I'm saying is not what happens with your project right now but it's something you should consider before giving the user control a responsibility it shouldn't have.

asp.net MVC 3 action method

I have created a partial view and I am using it in admin controller's Home action method. Inside partial view, I have an Account Controller with action method named Logon. I am trying to access main view's URL like this
if (Url.ToString().ToLower().Contains("/admin"))
return Redirect("/Admin/Index");
but I cant get the required result. How to get it admin/home ?
From the comments above it looks like you want to just access the current url. If so, then you can simply use the Request object. E.g.
Request.Url.AbsoluteUri
I wonder why you need this though... what is it you are trying to do exactly?

asp.net MVC3 View

I want to open a view that says update or insert successful. I did not create an action because I only want a simply status page. I created a view called Status.cshtml
I get a message saying that the view cannot be found. Any idea what is wrong?
Do I need an action for this to work?
When your process has finished successfully use the following line of code...
return View("Status");
Note: There is no action method with the same name - so navigating to www.yourdomain.com/YourController/Status will not route correctly...
Just a View is not enough. Your request first reaches a controller action through Asp.net mvc routing and only if the correct action is found the relevant view is displayed. Here is a video if you need a quick grab of how asp.net controllers and views work.

asp.net MVC giving a controller the name 'AdminController' makes it not work

I am working on a ASP.NET application that uses ASP.NET MVC.
I tried naming one of my controllers "AdminController" meaning I typed "Admin" in the new controller text box and it filled out the controller part all by itself of course.
This controller never worked until I changed it's name. If I changed the name to anything else it worked with no problems.
I looked inside my Global.asax.cs file where the routes were configured and I found no routes leading to it.
I tryed adding a route to this new controller like this:
routes.MapRoute("Admin", "calcul/SomeAction",
new { controller = "Admin", action = "SomeAction" });
and it worked but then mysite\admin would only get routed to that specific action.
I renamed the controller to AdminSection and it works but I don't understand why it didn't work before.
Does anyone have any idea
You shouldn't need to create an explicit route for your controller if the default route matches it (controller/action/id).
Also, in the new controller text box you would need to type AdminController rather than just Admin.
The MVC framework will look for classes that end with this when looking for possible controllers.
It was me, there was a area named admin that I didn't see. When I excluded it from the project the "adminController" controller started to work

Asp.Net MVC Not Duplicate forms when Edit/Add

When we have anything that requires user input (Eg adding a product to a database) the Edit screen looks the same as the add screen. When using MVC .Net how do you handle this? Do you return the same view? Just adjust the model to reflect the change?
Same (partial)view
You create only one but strong typed view (depending on the UI it can of course be a partial view as well). When adding new data return this view from controller action with default model object instance (usually just a new instance without any properties being set), but when you edit, return it with the object instance that you'd like to edit.
Controller part
Regarding controller actions you can have four of them:
Add GET
return View("SomeView", new Customer());
Add POST
Edit GET
return View("SomeView", new CustomerRepository().GetCustomer(id));
Edit POST
Bot GET actions return the same view but with different model as described earlier. POST actions both store submitted data, but return whatever they need to. Probably some RedirectToAction()...
You can use the same view for display and Edit, simply call it from your controller
return View("ViewName")
You could have the form fields in a partial view and have two separate views using the same partial view, one posting to the edit controller action method and the other posting to the add controller action method.
Partial views are used to remove duplicity. You could read an example of this in the Nerd Dinner tutorial.

Resources