I am getting the following error when trying to return a ViewResult from a post action:
The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.cshtml
~/Views/Shared/Index.cshtml
~/Views/Home/Home.cshtml
~/Views/Shared/Home.cshtml
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Home.master
~/Views/Shared/Home.master
~/Views/Home/Home.vbhtml
~/Views/Shared/Home.vbhtml
My view is definitely recognised because it works on the GET action.
The code that returns the ViewResult in the POST action is:
return View("Index", "Home", Model);
Here is the view.
Can anybody suggest why this would not be working?
A little more context:
The get action displays the view fine. The post action is actually to a different url but returns the same view. It's the post action that's causing the problem. Both GET and POST actions are on the same controller HomeController.
Here's the (stripped down) controller:
public class HomeController : Controller
{
[HttpGet]
public ActionResult Index()
{
return View(new LoginModelBase());
}
[HttpPost]
public ActionResult Login(UsernameLoginModel Model)
{
...
return View("Index", "Home", Model);
}
}
I've just realised what it is!. I'm using the wrong overload of the View(...) method.
It should be:
View("Index", Model);
Related
I want to call the "Delete Person" method from the "Home" controller on the link provided by the "Index" view, but get the error: "could not find this resource": HTTP 404, URL: /Home/DeletePerson/1. I tried #Html.ActionLink, but it doesn't work either. Where is my mistake?
The project has .NET Framework 4.7.2, Entity Framework 6.2.0, MVC 5.
This project has HomeController:
public class HomeController : Controller
{
...
public ViewResult Index()
{
...
return View("Index");
}
[HttpGet]
public ActionResult DelelePerson(int id)
{
...
return View(person);
}
[HttpPost]
public ActionResult DeletePersonConfirmed(int id)
{
...
return RedirectToAction("Index");
}
}
My Index.cshtml includes
<td><p>Del</p></td>
First, use Html.ActionLink to create the correct link They are aware of your specific routing configuration. Second, your link should probably look like "/Home/DeletePerson?id=#b.id", but that depends on your routing configuration which you haven't posted.
I'm new to MVC C# and I'm still learning the basics.
I was doing the guide at the link http://www.c-sharpcorner.com/UploadFile/ff2f08/multiple-models-in-single-view-in-mvc/
Way 6: "Using Render Action Method".
But when I Insert Object, Post results were repeated not stop. Help me!
HomeController:
public ActionResult Index()
{
return View();
}
public PartialViewResult ShowPost() {
.......
return PartialView(Posts);
}
public PartialViewResult SavePost()
{
return PartialView("SavePost", new Post());
}
[HttpPost]
public PartialViewResult SavePost(Post post)
{
if (ModelState.IsValid)
{
repository.Insert(post);
return PartialView("Index");//?????????
}
else
{
return PartialView("Index");
}
}
View
"Index" :
#{Html.RenderAction("SavePost","Home");}
#{Html.RenderAction("ShowPost","Home");}
"SavePost":
#model ERichLink.Domain.Entities.Post
#using (Html.BeginForm("SavePost", "Home",FormMethod.Post))
{
#Html.TextBoxFor(model => model.Title)
#Html.TextBoxFor(model => model.CategoryID)
#Html.TextBoxFor(model => model.Description)
<input id="post_btn" value="post"type="submit"/>
}
"ShowPost"
.....
RESULT: I can view Index Page successfully, but when I click submit, Post object insert to db repeat incessantly.
All child actions use their parent http method. So when you first call index method with get, child-renderactions makes http get request too. But when you submit and return index view, then all the child actions inside index view become post. So after submit, it calls http post save method. Then it returns index view. Then it calls again http post save...infinite loop. Best practices never return View() inside PostMethod.
#{Html.RenderAction("SavePost","Home");} executes public ActionResult SavePost()when rendered by any get method executes public ActionResult SavePost(Post post)([HttpPost]) when rendered by any post method.
[HttpPost]
public ActionResult SavePost(Post post)
{
db.Posts.Add(post);
db.SaveChanges();
return RedirectToAction("index");
}
When you make this time, it redirects index action and child-actions inside index view become get request not post.
I am trying to make a POST request from my View by calling an ActionResult in my Controller. Basically there are a list of events in the view and the user can view the details of the event by clicking the event. This part works. However, once they view the details they also have the ability to sign up for the event. This is the part which is not working.
A sample action I'm trying from the view:
#Html.ActionLink("SignUp", "SignUp", new {id = "2"}, null)
This should access this action result:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SignUp(int id)
{
if (ModelState.IsValid)
{
var registratie = new Registratie(User.Identity.GetUserId(), id);
_db.Registraties.Add(registratie);
_db.SaveChanges();
return RedirectToAction("Index");
}
return View("Index");
}
However, I am getting a 404 error. I think it can't find the actionresult?
The details action result is on the same page however and that works:
// GET: /EventPlanner/Details/5
public ActionResult Details(int id)
{
var evenement = _db.Evenementen.Single(e => e.ID == id);
return View(evenement);
}
I don't understand why the signup gives a 404. Any ideas?
You cant use ActionLink for making POST request. You have following alternatives.
Use submit button to post form
Use Ajax.ActionLink()
Use jQuery.ajax.
I would recommend submit button because I feel it is simpler than the rest.
As an example for first approach. Try this
Razor:
#using (#Html.BeginForm("ActionName", "ControllerName"))
{
<input type="hidden" name="id" value="2" />
<input type="submit" value="Post" />
}
Controller:
public class ControllerNameController : Controller
{
[HttpPost]
public ActionResult ActionName(string id)
{
//Your stuff
return View();
}
}
Its because your Detail Action method is a Get method while your SignUp Action method is decorated with [HttpPost] attribute, which means its a Post method. Remove HttpPost from your action method and it will run.
Edit:
For your purpose, I would recommend you use approaches #Lmadoddin Ibn Alauddin
suggested.
You can put your data under form tag and submit it using submit button(I don't recommend by looking at your code and you have not posted HTML too').
Or:
You can make $.ajax() call with type: 'POST' and pass your data like data: {id: 'idvalue'}.
Hope this will help you. Let me know if you face any problem.
In my mvc project i have a controller with the following actions:
public ActionResult Index()
{
return View(new List<Product>());
}
The corresponding index view render a partial view with the master grid:
#model System.Collections.Generic.List<TbbModels.Domain.Product>
#Html.Partial("_ProdutoMasterGrid", Model)
This partial view have a submit button and a grid. The client needs to put some data in the form and submit it to that action:
public ActionResult _ProdutoMasterGrid(string param)
{
return PartialView("_ProdutoMasterGrid",
repository.Compare(param).ToList());
}
But than i get the grid without the layout. How can i return a partial view with the layout?
You should explicitly return the correct view:
public ActionResult _ProdutoMasterGrid(string param)
{
return View("Index",
repository.Compare(param).ToList());
}
This ensures that when you do a post to this action, it returns the index view. I'm supposing here that repository.Compare return a List<Product> as well, since the type needs to match.
I have a strongly typed partial view that should show the name of an account that a user is logged into:
#model MyNamespace.Models.AccountNameViewModel
#if (Request.IsAuthenticated)
{
#Html.Action("AccountName", "AccountNameController", Model)
Logged in to #Model.AccountName
}
I have a controller:
public class AccountNameController : Controller
{
public ActionResult Index()
{
return View();
}
[ChildActionOnly]
public ActionResult AccountName(AccountNameViewModel model)
{
... Do somthing with the repository to populate the model
return PartialView(model);
}
}
What I want to do is add a shared partial view that displays the name of an account that a user is logged into. What I get is the following error:
The controller for path '/ParentViewPath/' was not found or does not implement IController.
Am I at least heading in the right direction?
You have to remove the controller part in your call
#Html.Action("AccountName", "AccountName", Model)
To render a partial view you can also call
#Html.Partial("AccountName", "AccountName", Model)