Error executing child request for handler - asp.net-mvc-3

I use a child action in Home Controller like this:
[OutputCache(Duration=30)]
public ActionResult ChildAction()
{
Response.Write("Date Time is="+DateTime.Now);
return View();
}
Home.cshtml
<h2>About</h2>
<p>
#Html.Action("ChildAction")
</p>
but after run my asp.net mvc3.0 Project i get an error :
The view 'ChildAction' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/ChildAction.aspx
~/Views/Home/ChildAction.ascx
~/Views/Shared/ChildAction.aspx
~/Views/Shared/ChildAction.ascx
~/Views/Home/ChildAction.cshtml
~/Views/Home/ChildAction.vbhtml
~/Views/Shared/ChildAction.cshtml
~/Views/Shared/ChildAction.vbhtml
please help me how can i solve it
thanks

You should have a view named "ChildAction", or in action you should return view as
return View("ViewName"); In summary, view name should be same as action name, not controller name as you did(You named view as "Home").

Related

mvc4 Razor Ajax Call to show partial view

Am trying to get a partial view to render with an AJAX call. The following ActionResult is in my base controller which is inherited by all other controllers in the solution:
public ActionResult FileManager()
{
return View("_FileManagerPartial");
}
and the folowing code is another partial that sits on the page
#Ajax.ActionLink("File Manager", "FileManager", new AjaxOptions { UpdateTargetId = "dvFilemanagerContainer" });
dvFilemanagerContainer is a div in the layout view and the partial view "_FileManagerPartial.cshtml" is in the shared views folder.
When I click the link for the ajax call, instead of loading the intended partial view it loads a duplicate of the page into the div.
Any ideas?
Edit
PartialView contents its currently just the following
<div id="dvFilemanagerWrapper">
File Manager
</div>
change your controller to
public PartialViewResult FileManager()
{
return PartialView("_FileManagerPartial");
}
In View add this line, so that with partial view, the master layout is not rendered, only partial view is rendered:
#{
Layout = null;
}
<div id="dvFilemanagerWrapper">
File Manager
</div>
Problem was to do with how my RouteConfig was handling the call. Have created a new MapRoute to point it to the right place and is now working. Thanks for help guys.

ASP.NET-MVC3: how to get the url of an action in the controller

In an ASP.NET MVC3 application
I have an Action method that returns a file for the user to download.
In the HomeController:
[HttpGet]
public ActionResult DownloadFile()
{
pathToReturn = FileManager.GetPathOfFile();
return File(pathToReturn, Path.GetFileName(pathToReturn));
}
I call it when he clicks a button in the view:
<input type="button" onclick="window.location.href='#Url.Action("DownloadFile", "Home")';" value="Download File"/>
How can I access this action from the address bar? (I want to remove the button)
what is the url for that action? (What should I type in the address bar in order for the file to be downloaded? is this possible to do?
Thanks
Let say that it depends on your routing rules... but supposing you're using the default ones, you should access to your action with the url:
http://yourserver/yourapplication/Home/DownloadFile
http://yourserver/home/DownloadFile
provided that routing you have not changed

MVC 3 Partial View and what I need to send to it?

I am wondering about a couple variations of forms and partial forms. The submit is on the parent page and I have varied what I pass to the partial view. I have a parent view with a related HomeViewModel (which has public properties Name and public Person Employee {get;set;}
1.) Scenario 1: The main view has something like the following
#model MasterDetailSample.Models.HomeViewModel
#using (Html.BeginForm()) {
<div>
#{Html.RenderPartial("_PersonView", #Model);}
</div>
<input type="submit" value="Save" />
}
In this scenario I am passing to the partial view _PersonView the entire HomeViewModel. Within _PersonView partial view I have to reference a property of the HomeViewModel i.e. Person object via #model.Employee.Name (in this scenario the submit is on the parent form (not within the partial view))
When I hit submit on the form (POST) in the controller i have to access the property of Employee "Name" via the following model.Employee.Name
This seems to work however notice the following variation scenario 2 (where I only pass to the partial the Employee object)
2.) Scenario 2
In this scenario I only want to send the Employee object to the partial view. Again the begin form and submit is on the parent form.
So from the parent form i have
#{Html.RenderPartial("_MasterView", #Model.Employee);}
and so within the partial view i reference the Name property of the Person object via #Employee.Name Now when I submit the form within the controller the Employee object is not available from the auto model binder. I can access the properties via formcollection but not from the model parameter
i.e.
[HttpPost]
public ActionResult Index(ModelViewModel model) {
**//model.Employee is null!**
return View();
}
Why? (is model.Employee null) I would like my partial view to only accept an object of type Person however after submitting from the parent page, the Employee property is null. On the partial view I am using the following on the #model line
#model MasterDetailSample.Models.Person
I would like the partial to only need a Person object to be sent to it, but I would like the submit on the main form. If i do it this way I can re-use the partial view in a few situations however IF i must send HomeViewModel i have significantly limited how I can use this partial view. So, again, I only want to use Person as the model with the partial view but I need to be able to access the properties when submitted from the parent view.
Can this be done? If yes how?
thx
You have a couple of options:
1) One I recommend -> Dont use partial views, instead use EditorFor and create an editor template for Person. With partial views the context is whatever model you pass into the view, this is why your example (1) works and (2) not. However with editor templates the parent context is taken into consideration with the html helpers and will generate the correct input names. Have a look at Darin Dimitrov's answer to a similar question.
2) Use your second example as is, but change the post action to look something like this:
[HttpPost]
public ActionResult Index(ModelViewModel model) {
TryUpdateModel(model.Employee);
**//model.Employee should now be filled!**
return View();
}
3) Use custom html helpers that accepts prefix for input, see this answer I posted a while back for example code. You could then use this inside your partial view.

MVC 3 POST data and the Id field

I have a strongly typed razor view for a model in my MVC 3 project. Basically its for editing the model.
The model contains an Id field for the database key and some other string fields (Its a viewModel and all but thats not the point of the question).
In the view I just have a form and a submit button and nothing else. When the View is posted to the controller the model in the controller has all fields empty EXCEPT for the Id field which seems to have been auto-magically filled up.
How and where does the Id field gets populated in the model without there being a corresponding 'input' element for it in the view.
This is probably a dumb question but I would appreciate even just a link to what I should read up on. Thanks.
I bet it comes from the url as route parameter.
For example you have the following controller:
public class HomeController: Controller
{
public ActionResult Index(int id)
{
vqr model = GetModel(id);
return View(model);
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
// the model.Id property will be automatically populated here
// because the request was POST /home/index/123
...
}
}
and the following view:
#model MyViewModel
#using (Html.BeginForm())
{
<button type="submit">OK</button>
}
Now you navigate to GET /home/index/123 and you get the following markup:
<form action="/home/index/123" method="post">
<button type="submit">OK</button>
</form>
Notice the action attribute of the form? That's where the id comes from. Basically the Html.BeginForm() helper uses the current url when generating the action attribute, and since the current url is /home/index/123 it is what gets used.
And because if you have left the default routes in your Global.asax, the {id} route token is used at the end of the url, the default model binder successfully binds it to the Id property of your view model.
You are probably hitting a URL similar to the following: /MyObject/Edit/15
This is then returning the page that you have your blank form on.
What happens next is you have an HTML.BeginForm() which is posting BACK to /MyObject/Edit/15
Now because of the post back having the same format your routing rules are picking up the '15' and binding it back to your id.
Have you added the ID field as a hidden field?
e.g.
#Html.HiddenFor(x=> x.ID)

strange behavior of MVC3

I created a custom Model i.e. to support my Razor View. Then I created a controller as following`namespace MyCandidate.Controllers
public class CandidateViewModelController : Controller
{
//
// GET: /CandidateViewModel/
public ActionResult Index()
{
return View();
}
}
I also have the following statement in my _Layout.cshtml
#Html.ActionLink("Canid", "Index", "CandidateViewModel")
Next i created a view and the very first statement of the view is
#model MyCandidate.Models.CandidateViewModel
when i run my project i get the following error
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
I spent more than 3 hours but could not figure out?
your Index() not get any parameters, but you send "CandidateViewModel") add Index(string input) method with attribute [HttpGet] to controller.
this error meen you haven't view "Index" in "Views/CandidateViewModel/Index.cshtml".
Maybe you delete master page files (_ViewSrat, _Layout)
Or you made a mistake when you change your routes
Change your ActionLink to:
#Html.ActionLink("Canid", "Index")
If you want to pass any data to View , you can also use ViewBag:
// Controller :
ViewBag.CandidateValues = CandidateViewModelData;
// View
#Html.Label("LabelName", (CandidateViewModel) ViewBag.CandidateValues.FiledName);

Resources