How to navigate among the views in mvc - asp.net-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();
}

Related

MVC architechture views and controller

You can tell which controller is serving the view, by looking at the code, and also the url in asp.net.
is there any other way to tell which controller is creating the view besides the two way i mentioned?
View depends on Actions in Controller. For example if you have a controller and the Action like below,
public class HelloWorldController : Controller
{
public ActionResult Index()
{
return View();
}
and in the Views/HelloWorld folder a file Index.cshtml then you can say Index.cshtml view is for Index action of HelloWolrd Controller. It works as follows,
There would be subfolder with the name of the Controller in the Views Folder
There would be actionname.cshtml file representing view of a particular action
Hope it helps
A nice, short snippet, to fetch the controller name in the view, is this one:
#ViewContext.RouteData.Values["controller"]
Just use it in your view, where needed.

How do I pass a model to a partial view and load a specific record?

I am still relatively new to MVC and am finding every new concept to be a struggle, so please forgive me if this is an overly simple concept or the question has been asked many times before (I tried to find other examples).
I have several modals that can be called from my shared layout using jQuery's "dialog." Each modal is simply a DIV with a partial view attached to it like this:
<div id="JoinDialog" title="Join the Contractor Network" style="display: none;">
#Html.Partial("_JoinPartial")
</div>
And is called like this:
$(".ClickToJoin").click(function () {
$(function () {
$("#JoinDialog").dialog({ width: "auto", height: "auto"});
});
});
I have added a "Profile" modal to the layout in which I would like to insert the user's data into the INPUT values. To do that, I presume that I will need to pass in a model, and load the data I want via the controller. Since I currently have this partial view in the "Shared" folder, I assume I will also need to move it to one of my view folders where I can attach it to a controller?
Any nudge in the right direction would be appreciated.
Since I currently have this partial view in the "Shared" folder, I
assume I will also need to move it to one of my view folders where I
can attach it to a controller?
No there is no need for you to move the partial view to the controller folder. You can use the partial view from the shared folder itself (View Engine also looks at Shared folder to find a matching view). Here goes the sample example -
Lets say you have a model like this -
public class MyModel
{
public string Name { get; set; }
}
And then you have an action to return the partial view from the shared folder -
public ActionResult GetPartial()
{
MyModel model = new MyModel();
model.Name = "Rami";
return PartialView("TestPartial", model);
}
Then have the partial view in the Shared folder like this -
#model YouModelNamespace.MyModel
<div>#Model.Name</div>
Then on the actual page, you can have following code -
#{
ViewBag.Title = "Index";
}
<h2>Index</h2>
#{
Html.RenderAction("GetPartial");
}
That will display the result from the partial view on the page as shown in below screenshot.
When you have to render a View (or a Partial View), asp.net mvc has some default orders to find it. First asp.net mvc will search the respective views' folder of the controller you are executing and if it was not found, asp.net mvc search on the Shared folder. So, if you have a view called _JoinPartial on the Views/Product (for sample) folder and shared folder, it will priorize the View folder. Sometimes you get a exception that views was not found, in there message you can see all places where asp.net mvc find it, for sample:
In your case, the controller could return a Partial View
public ActionResult GetJoinPartial()
{
return PartialView("_JoinPartial");
}
Since you have on the View folder, it will use it, instead it will use the partialView on the Shared folder.

using update panel in ASP.NET MVC 4

Please explain me how to create update panel in ASP.NET MVC4 application. I looked for many blogs... but can not find any useful way.
This is my view
How I can separate these actions in same view?
Your two panels don't allow the user to switch between one or the other so I assume that you have an "intro" view with the option to either Sign in or Register. Right? In that case there is no real need for client side panel switching using Javascript/Ajax. Your "intro" view can pass a parameter to the controller action defining whether it needs a Sign In or Register view back.
For instance:
// RouteConfig.cs
routes.MapRoute(
name: null,
url: "login-register/{action}",
defaults: new { controller = "Authentication"},
constraints: new { action = #"(SignIn|Register)" }
);
// AuthenticationController.cs
public ActionResult SignIn()
{
...
return View(); // Will return the Authentication\SignIn.cshtml view
}
public ActionResult Register()
{
...
return View(); // Will return the Authentication\Register.cshtml view
}
Update panel does not really exist in ASP.NET MVC. It used to be there in ASP.NET Web form develeopment world before people actually realized it is better to use hand written jQuery ajax for doing the partial page update.
You may use jQuery ajax methods to post your form data to an action method and do partial page update to the page as needed. You may also consider using Partial view (to return a part of a page) as required.
In your case you can create 2 partial views for Sign in and Register and include those in your main view, to make your code more reusable.
<h1>Login or Register</h1>
<div>
#Html.Partial("Login")
</div>
<div>
#Html.Partial("Register")
</div>

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

Can I share the same view for a create and edit in MVC3

I have a fairly complex view that's almost the same for the create and edit functionality in MVC3.
Every time I change one I have to remember to make the same changes in the other.
Is there a way that I can share a view between create and edit. For example can I have two view files with different names and link them or is there another even better way.
thanks
Marcel
You could simply make a partial view with your form contents and include this partial view in your create and edit view. With that, you you are able the have some differences in your views (maybe headline "edit" / "create").
#Html.Partial("FormView")
On the other side, you could specify your view in your controller action.
public ActionResult Create()
{
return View("CreateEditView");
}
public ActionResult Edit()
{
return View("CreateEditView");
}

Resources