I can change the method name but can anyone tell me why this doesn't work? How can i make it working without changing method name. Also I don't want to specify the action name in the view. Is it possible?
[HttpGet]
[Route("AddUpdateCategories/{storeId}")]
public ActionResult AddUpdateStoreCategories(int storeId)
{
return View();
}
[HttpPost]
public ActionResult AddUpdateStoreCategories(int StoreId,int[] ShopCategoryId)
{
return null;
}
Problem is post action is not getting called on submit.
You don't have to change the method name. The problem is that this post action has no route. If you use attribute routing, you have to specify a route for each action. Both of these actions would end up with the same route attribute, but the [HttpPost] attribute is enough for the routing framework to work out which one to use.
[Route("AddUpdateCategories/{storeId}")]
public ActionResult AddUpdateStoreCategories(int storeId)
{
return View();
}
[HttpPost]
[Route("AddUpdateCategories/{storeId}")]
public ActionResult AddUpdateStoreCategories(int StoreId,int[] ShopCategoryId)
{
return null;
}
Related
I understand ActionResult is the base class for RedirectResult class so essentially functionality for the code below is the same.
Is there an advantage to explicitly specify the appropriate result class as a return type as opposed to the base class?
public ActionResult Index()
{
return Redirect("Home/Contact");
}
public RedirectResult Index()
{
return Redirect("Home/Contact");
}
if you need redirect quickly to another action without lost any data and this action is in the same controller , call the action as a method:
public IActionResult Index()
{
return Contact();
}
public IActionResult Contact()
{
....
}
which one from another redirections methods is better is an opinion-based question and is not allowed by SO policy
Is it possible to have 2 ActionResult DeleteConfirmed in 1 controller?
I have 2 different views that I want to delete.
Thanks,
EB
[HttpPost, ActionName("DeleteLink")]
public ActionResult DeleteConfirmed(int id)
{
Link link = db.Links.Find(id);
db.Links.Remove(link);
db.SaveChanges();
return RedirectToAction("OutOfBank");
}
It seems that you want to overload DeleteConfirmed action. You can use the attribute if you want your code to do overloading.
[ActionName("MyOverloadedName")]
But, you'll have to use a different action name for the same http method.
So it's just semantics at that point. Would you rather have the name in your code or your attribute?
Code Example:
public class HomeController : Controller
{
public ActionResult GetEmpName()
{
return Content("This is the test Message");
}
[ActionName("GetEmpWithCode")]
public ActionResult GetEmpName(string EmpCode)
{
return Content("This is the test Messagewith Overloaded");
}
}
Phil has an article related to this: http://haacked.com/archive/2008/08/29/how-a-method-becomes-an-action.aspx
I am creating an MVC 5 application. I am using Rotativa to generate PDFs
they have a method called
public ActionAsPdf(string action, object routeValues);
I am having trouble to direct to POST method of an action
this is that GET and POST actions
[HttpGet]
[ValidateInput(false)]
public ActionResult Create_Brochure(IEnumerable<ProductsPropertiesVM> model)
{
.............
return View(selectedIDs);
}
[HttpPost]
[ValidateInput(false)]
public ActionResult Create_Brochure(string m)
{
return View();
}
Once I run this program its directing to GET method but I want to direct to POST action
using following method
public ActionResult PrintIndex()
{
return new ActionAsPdf("Create_Brochure") { FileName = "Test.pdf" };
}
You need to match the parameters of the POST version of Create_Brochure:
return new ActionAsPdf("Create_Brochure", new List<ProductsPropertiesVM>())
{
FileName = "Test.pdf"
};
Of course, you'll have to pass the correct model data instead of the List<ProductsPropertiesVM>.
I have a controller action Foo. From within Foo I need to pass a GUID to another action called Index. how can I do that?
public ActionResult Foo() {
return View("Index",someguid);
}
public ActionResult Index(Guid id)
How can I do that?
Precisely what the RedirectToAction method is for.
As Dom mentioned in the comments, this should get you going in the right direction.
public ActionResult Foo() {
return RedirectToAction("Index", new { id = someguid });
}
Maybe the answer to the following is simple, but I have hard time finding the answer:
When I have a GET method in a controller that is secured with the [Authorize] attribute, and a POST method (defined with [HttpPost]), will the same restrictions apply to it as well? Both methods have the same name, but differ in parameters.
Example code:
[Authorize(Roles = "Administrator")]
public ActionResult Delete()
{
return View();
}
[HttpPost]
public ActionResult Delete(int id)
{
/* the method's logic omitted */
return RedirectToAction("Index");
}
No, the controller considers these two separate actions (since that's what they are), and as such, don't share restrictions.