New view does not appear in the browser. - asp.net-mvc

I am using MVC.NET, using aspx view engine. I have created a new view, at visual studio, under HOME directory named jobs.aspx.
When I go to properties, it shows "Browse to URL: ~/Home/jobs".
But When I add
Localhost:port/Home/jobs at the browser I get 404 error.
While Localhost:port/Home works normally.
Do you know how I can fix that?
Thanks

You need to add a Jobs() action to your HomeController.cs to complete the loop. Notice the Index() action in that class.
http://www.asp.net/mvc/tutorials/older-versions/getting-started-with-mvc/getting-started-with-mvc-part1

In addition to SethMW's answer.
Go to your HomeController class and add ActionResult that will return a View.
This action result should be called Jobs() and may return a specific or default view.
public ActionResult Jobs()
{
return View();
//return View("NameOfDesiredView");
}
Also if you are getting started with MVC I suggest you to learn the Razor syntax rather than the outdated aspx view engine.

Related

How to view MVC .cshtml file as html?

How exactly does one render a .cshtml file?
I'm new to ASP.NET. I've created an MVC project using Visual Studio's template. Apparently all the templates have .cshtml for the Default / Index files. But my server gives me this error when I try to view it:
Server Error in '/' Application. This type of page is not served.
Description: The type of page you have requested is not served because it has been explicitly forbidden. The extension '.cshtml' may be incorrect. Please review the URL below and make sure that it is spelled correctly.
Requested URL: /mobile/WebApplication1/Index.cshtml
So, how exactly do I view the Index file? Do I need to pass it through something to convert to .html or render it somehow?
Files with extension .cshtml in the context of ASP.NET MVC are views. They cannot be viewed (served by the web server) just by themselves. You need a controller action that will render the view.
NOTE: you could employ some "tricks" to modify IIS settings and your app to enable serving .cshtml files to browser requests, but this would not be normal behaviour.
Assuming your view is located at ~/Views/Index.cshtml here is a (trivial) example of a simple controller action:
public class ExampleController : Controller
{
public ActionResult Index()
{
return View();
}
}
This controller should be placed at: ~/Controllers/ExampleController.cs
You will access the rendered page at: localhost/example/index
More about ASP.NET MVC here: http://www.asp.net/mvc/overview/getting-started/introduction/getting-started
You can not access an MVC view directly by its name. Ever...!
What you can do is call a Controller action, and that is done through an URL such as /WebApplication1/Index.
The above URL means two things:
there has to be a class called WebApplication1Controller.cs,
and that class must have a method public ActionResult Index().
The method Index will then determine which View (if any) is going to be displayed, and also what data will be sent to that view to use.
If the method Index executes return View(); then there is an implicit rule that the view with the same name (in this case Index.cshtml) is going to be shown.
Or if for some reason Index executes return View("Wow"); instead, then the view called Wow.cshtml will be displayed.

How to navigate among the views in mvc

i am new in mvc. due to lack of knowledge i am not being able to do one thing.suppose i have view Index.cshtml and this view reside in home folder. i have register folder in home folder and in register folder there is view called register.cshtml. i have another folder called catalog in home folder. when i will run my application then by default Index view will render and there will be two button or two link button. one is button text is Catalog and another button text is register.
when user click on register button then register view should load and when user click on Catalog button then Catalog view should load. how could i do this ? what kind of code i need to write and what kind of code i need to write for mapping in global.asax file ?
another question is that how could i pass my model or view model too when navigate from one view to another view.
looking for help & concept with sample code. thanks
Considering your question, I've come up with the idea that your knowledge about web applications comes from ASP.NET that folders are used to categorize different area in a web application. If I were right, you should map folders in ASP.NET with Controllers In ASP.NET MVC (it is not good analogy, but for starting is helpful). In this way, you would have three Controllers or one Controller with three Actions. I am going to choose second one.
Controller:
public class HomeController : Controller
{
public ActionResult Index()
{
var model = new TheViewModel();
return View(model);
}
public ActionResult Register()
{
return View();
}
public ActionResult Catalog()
{
return View();
}
}
View:
#model MvcApplication1.ViewModels.TheViewModel
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#Html.ActionLink("Register", "Register")
<br/>
#Html.ActionLink("Catalog", "Catalog")
Your second question has answered at Passing ViewModel in ASP.Net MVC from a View to a different View using Get
Call the appropriate /controller/action in your respective button click handlers.
In your case for the register button handler direct it to /home/register.
Have a view for your register functionality.
In the register action of your home controller return the view you want to show.
public ActionResult Register()
{
return View();
}

MVC view not showing

I'm building a site in MVC 4. After the first view (i.e., home page) shows, I redirect (after some other things) to another view:
return RedirectToAction("Index", "ClaimsSearch", new { carrier = carrier });
A breakpoint in that view actually gets hit, and the parameter even has the value:
public class ClaimsSearchController : Controller
{
public ActionResult Index(string carrier)
{
return View();
}
}
I created a new view (for ClaimsSearchController) by right-clicking on Index, then "Add View." However, after "return View()" executes, the browser still just has the original view sitting there. The new view never appears. By the way, I can type in the second controller name (localhost:1234/ClaimsSearch) and this view DOES show up.
Why does this action not actually show the view?
Returning View without overloads will default it to look for a file based on the action it is within - For this instance it will be looking for Index.cshtml
It is going to be looking within your View folder, if this controller is within a subfolder of this it'll be looking there for Index.cshtml
By assumption... View/ClaimsSearch/Index
OR it'll be looking within 'View/Shared/'
My suggestion:
Create a html view file called what it is looking for (Index) or overload the return function to specify another file to look for.
But regardless, I think you're going to have to make an cshtml file within View/ClaimsSearch

ASP.NET MVC partial view that updates without controller

I have a partial view that shows a list of Categories. I'd like to put that partial view on any page, but I'd like to have it to call to the service and get a list of categories by itself without me having to do that in every controller action. Something like webforms in which you can put a code-behind on it.
For eg.
Actions
public ActionResult Index()
{
JobListViewModel model = new JobListViewModel();
model.Categories= jobService.GetCategories();
return View(model);
}
public ActionResult Details(int id)
{
Job job = jobService.GetJob(id);
return View(job);
}
I created a partial that will take the model.Categories model and display a list. As you can see, the Index page will work fine, but I do not want to call it again in the Details page. Is there a way to make my partialview call to the GetCategories() service by itself?
Use Html.RenderAction - that gives the partial view its own controller action.
You should also mark you partial action with the attribute [ChildActionOnly].
DVark,
As noted in the accepted answer, for your scenario, RenderAction is the most appropriate.
I thought I'd link a little article that distils my thinking on the topic (i.e. when to use RenderPartial vs RenderAction):
http://cbertolasio.wordpress.com/2010/09/21/mvc-html-renderaction-vs-html-renderpartial/
hope it helps
[edit] - as an aside. a year or so ago, i got myself into a few scrapes by not appreciating the power of RenderAction, in favour of RenderPartial. as a result, i had littered the shared view space with lots of partialviews in order to access them from a variety of sources. the moral of the story: know your 'territory' before planting your flag.

ASP.NET MVC - Go to a different view without changing URL

is it possible to go to a different View without changing the URL? For example in my Index View, I have a link to go the the Details View but I would like to keep the URL the same.
Thank you very much,
Kenny.
As already mentioned, you could make the Details link an Ajax.ActionLink and use this to change the content of a div.
Failing that, the only other way I can think of doing it is by making your details link a button and POST to your index action. You could apply CSS to the button to make it appear more like a normal html link.
public class HomeController : Controller {
public ActionResult Index() {
return View("Index");
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Index(int hiddenInputFieldId) {
return View("Details");
}
}
EDIT:
Based on JonoW's comment, you'll have to pass in a 'fake' param with your post, this is not really a problem though, you can just use a hidden input field for it.
You can return the same view from multiple controller actions, but each controller action requires a unique URL:
public class HomeController : Controller {
public ActionResult Index() {
return View("home");
}
public ActionResult About() {
return View("home");
}
}
If you want a link to load up content from a different page without changing the URL, you'll have to use some Ajax to call the server for the content and update the parts of the page you need to change with the new content.
I don't know why you would like to do that, but you could have an Ajax.Actionlink which renders the Details View..
There is almost no reason to hide an URL, not sure what you would like to to.. maybe you explain further that someone can give a better approach.
You can use a good old Server.Transfer for this. However, I'd suggest doing it like has been detailed in this SO post. This gives you an easy way to return an ActionMethod from your current action without peppering your code with Server.Transfer() everywhere.
You can do this by rendering partials- I do this to load different search screens. Sample code is as follows (this is slightly different to my actual code, but you'll get the idea):
<% Html.RenderPartial(Model.NameOfPartialViewHere, Model.SomeVM); %>
Personally though, I don't see why you don't just change the URL?

Resources