I am attempting to make a simple test website to allow me to list, create, edit and delete customer objects using MVC4.
Inside my controller I have 2 create methods, a Get for when the form loads with the controls, and a Post that actually saves the data.
//
// GET: /Customer/Create
[HttpGet]
public ActionResult Create()
{
return View();
}
//
// POST: /Customer/Create
[HttpPost]
public ActionResult Create(Customer cust)
{
if (ModelState.IsValid)
{
_repository.Add(cust);
return RedirectToAction("GetAllCustomers");
}
return View(cust);
}
However when I run the project and attempt to access the create action I get an error that:
The current request for action 'Create' on controller type 'CustomerController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult Create() on type [Project].Controllers.CustomerController
System.Web.Mvc.ActionResult Create([Project].Models.Customer) on type [Project].Controllers.CustomerController
My I understand that it can't see the difference between my Get and Post methods, but I have added the attribues. What could be the cause of this and how can I make it work again?
MVC does not authorize you to have 2 action methods with the same name.
BUT you can have 2 action methods with the same URI when the http verb differs (GET, POST). Use the ActionName attribute to set the action name. Don't use the same methods names. You can use any name. A convention is to add the http verb as the method suffix.
[HttpPost]
[ActionName("Create")]
public ActionResult CreatePost(Customer cust)
{
if (ModelState.IsValid)
{
_repository.Add(cust);
return RedirectToAction("GetAllCustomers");
}
return View(cust);
}
Related
I having a question should we assign a http attribute for each action?
Like for example Index Page that doesn't have any action just displaying html we still need to assign a http attribute? Why since there is no retrieving data.
And If I remove Http Attribute for ViewDetail and CreateRecord, the page is still working and no bug what the huge difference for adding and not adding http attribute
public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult ViewDetail()
{
//.... Get Data Action
return Redirect(Url.Action("Edit","Home"));
}
[HttpPost]
public ActionResult CreateRecord()
{
//.... Create Action
return Redirect(Url.Action("Edit","Home"));
}
There may be some methods in your Controller class that are not HTTP Endpoints.
I have a Search controller with a generic Search() action that takes several parameters and has a bunch of logic. I'd like to call this from other controllers without having a lot of copy/paste code.
I'd like to call this action from these different urls/controllers.actions.
/Search/Search?text=mySearchText
/User/SearchTransactions?type=purcahse
/Transactions/UserSearch?UserId=1
I could move the method to a baseController but I'd have to call /Search on each controller and I'd like to have them all named differently.
you already have your solution man put it in base controller and decorate it with actionName attribute
like
[ActionName("Search1")]
public ActionResult SearchText(string text) {
return View();
}
[ActionName("Search2")]
public ActionResult SearchType(string Type) {
return View();
}
[ActionName("Search3")]
public ActionResult searchId(int ID) {
return View();
}
now you can do like
/search1
/search2
/search3
When I load the default page (http://localhost/MVCP/Home/index) it loads correctly, whereas when I load another view (http://localhost/MVCP/Home/Create) it doesn't load. How can I fix this?
My Create action in HomeController:
[HttpGet] [ActionName("Create")] public void Create() { }
Q: Do you have an action in your HomeController called Create?
A: Yes, [HttpGet] [ActionName("Create")] public void Create() { }
Your action return value is void and probably you even didn't write anything in response. change the signature of action to have an ActionResult as return a View.
public ActionResult Create()
{
return View();
}
To learn more:
Adding a View
in Getting Started with ASP.NET MVC 5 Series.
May be there is no view you have created so far and it seems your controller are inside area folder so have u checked your routeing too.
I think your Action should return ActionResult or ViewResult but certainly not "void" as you have written currently.
and also you should write
return view();
in Create action
i am trying to redirect to another action within the same controller
action is called index
[HttpGet]
public ActionResult Search(string city)
{
return RedirectToAction("Index", "Rentals", new { CityName = city });
}
this is index action
[HttpPost]
public ActionResult Index(String CityName)
{
}
am i missing something?
You are trying to redirect action which is searching for a matching action but in this case there is no get action, so you have to add a get method to accept redirect. If you want, you can check the HTTPGET or POST inside the method
[HttpPost]<---- Remove this
public ActionResult Index(String CityName)
{
}
Please change HttpPost to HttpGet
[HttpGet]
public ActionResult Index(String CityName)
{
}
Because whenever you call the Action, then the GET method will be first called.
As the two actions are in the same controller, you could call the Index method directly from Search like this:
return Index(city);
not necessarily to user the RedirectToAction method.
I'm using ASP.MVC and trying to learn...
I have the following controller
// get all authors
public ActionResult Index()
{
var autores = autorRepository.FindAllAutores();
return View("Index", autores);
}
// get authors by type
public ActionResult Index(int id)
{
var autores = autorRepository.FindAllAutoresPorTipo(id);
return View("Index", autores);
}
If I try http://server/Autor/1 I get a 404 error. Why is that?
I even tried to create a specific method ListByType(int id) and the correspondent view, but that does not work too (URL: http://server/Autor/ListByType/1)
Any ideas?
EDIT Oh, the http://server/Autor works just fine. The method without parameters is spitting out my view correctly.
Assuming your class is called AutorController, and assuming you have the default route configuration of
{controller}/{action}/{id}
You should be able to request
/Autor/Index/<anything>
However, you seem to be a bit confused on the action methods. You could combine your action methods like so:
public ActionResult Index(int? id)
{
var autores; // I know this wont compile - but without knowing what type FindAllAutoRes returns, I can't make a specific type for this example
if(id.HasValue)
autores = autorRepository.FindAllAutoresPorTipo(id);
else
autores = autorRepository.FindAllAutores();
return View(autores); // Will automatically select the 'Index' View
}
MVC will select the first valid action method that corresponds to your route data - so if you request /Autor/Index/3, you will get the first action method, but since it has no parameters, the id route data is not bound to anything.