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
Related
I have some action's views that are dependant on settings from DB (for example display textbox or not). Settings are changed in admin controller.
What I'd like to achieve is to have the preview of the changed views in admin's sidebox (right side of the screen) after the changes will be saved to DB.
Is there a way to get View result of another action (casually returns View()) with button (Save and Preview) click in form of string (HTML) to display in the sidebox?
Or maybe has anyone other and better idea?
Yes. They're called child actions. Simply, you just call the action you want rendered via Html.Action:
#Html.Action("SomeAction", "SomeController")
However, there's a couple of things to keep in mind. First, your child action should return PartialView. Otherwise, you'll get the full layout rendered again where you call the action. If you want to use the same action for both a regular view and as a child action. You can branch your return:
if (ControllerContext.IsChildAction)
{
return PartialView();
}
return View();
Second, if you only return PartialView, then the action should not be available to directly route to. Otherwise, someone could enter a URL in their browser to go to this child action and only the partial view would be returned, devoid of layout. You can prevent this from occurring using the ChildActionOnly attribute:
[ChildActionOnly]
public ActionResult MyAwesomeChildAction()
{
...
}
Then, this action will only be available to be called via Html.Action.
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.
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();
}
so in my profile controller page.
I have a method call create
inside the create method
if (Convert.ToInt32(calBMI) >= 25)
{
return View("Index", Survey);
}
I want to render the page to index of survey(survey is another controller take care of surveys), how do i do it to get it works,thanks!!
return View("~/Views/Survey/Index.cshtml", objSurvey);
Assuming objSurvey is your model/ViewModel object and Survey/index view is strongly typed to the type of objSurvey Model/ViewModel
EDIT : As per the comment, If your view is not strongly typed, you can ignore the second parameter
public ActionResult GetSomeThing()
{
return View("~/Views/Survey/Index.cshtml");
}
If your intention is to share this view among multiple controllers, it should be in the /Views/Shared/ folder. There is a lack of good reasoning to use a view outside of either the controller folder or the shared folder.
It seems to me that you can just redirect to list of surveys (if that's your intent).
return RedirectToAction("Index", "Survey");
Say there's HomeController with an Details-action. return View() will send data to the Detals.aspx in the Home folder. But who makes that binding? and what if I want it to go to Edit.aspx instead?
Background:
Alot of the code in Details.aspx and Edit.aspx is identical, save for one textbox. Maybe by MVC rigor, the view is not supposed to make that kind of decisions, but hey, there's got to be a limit.
You can make it go to Edit.aspx by specifying it as a parameter of the View() function.
return View("Edit");
As to who makes the actual binding happen, it's the View Engine. It receives the returned ViewResult and analyzes it to see which template file to load and display. When it gets the string "Edit", it runs a find routine, using the context of the controller, to search a number of directories for filenames that match the convention. It starts in the controller's View directory, and then searches the Shared directory.
If you want to Edit.aspx to be rendered you could return View("Edit");