[英]Adding Create method in ASP.NET MVC
When i add 2 Create Method in my controller like below 当我在控制器中添加2 Create Method时,如下所示
[AcceptVerbs(HttpVerbs.Get)]
public ActionResult Create()
{
Product.Models.Product p = new Models.Product();
//update DB
try
{
return RedirectToAction("GetAll");
}
catch (Exception)
{
return View(p);
}
}
//
// POST: /Product/Default1/Create
[AcceptVerbs(HttpVerbs.Posr)]
public ActionResult Create(FormCollection collection)
{
try
{
if (myProduct.Products == null)
{
myProduct.Products = new List<Models.Product>();
}
Product.Models.Product p = new Product.Models.Product();
p.Name = collection["Name"];
p.ProductType = collection["ProductType"];
p.Id = myProduct.Products.Count + 1;
myProduct.Products.Add(p);
return RedirectToAction("Index");
}
catch
{
return View();
}
}
If i Comment GET action verbs and run application, application throws error resource not found. 如果我注释GET动作动词并运行应用程序,则应用程序将抛出未找到的错误资源。 It will not fire Create Action.
它不会触发“创建操作”。 my html has @using (Html.BeginForm())
我的html有@using(Html.BeginForm())
I Changed Form to even then i get same error. 我将表单更改为即使这样我仍然遇到相同的错误。 If i uncomment GET action verb, then always GET method fires up.
如果我取消注释GET动作动词,则始终会触发GET方法。 I want call POST Create action .
我想打电话给POST Create action。 Can any1 directme how to solve.
可以指导我怎么解决。
I have Product has Areas in my MVC project. 我的MVC项目中有“产品包含区域”。 within in it has ProductsController.cs Please help me how to call POST action Create method.
其中包含ProductsController.cs,请帮助我如何调用POST操作Create方法。
-Mahender -马亨德
Try following: 请尝试以下操作:
@using (Html.BeginForm("Create", "Default1", new { area = "Product" }, FormMethod.Post)) {
// Your Form
}
And please use 并请使用
[HttpGet]
[HttpPost]
instead of AcceptVerbs, its much cleaner. 而不是AcceptVerbs,它更干净。
[HttpGet] // Or even if you dont put this, it will be treated as GET
public ActionResult Create()
{
// your code goes here
}
[HttpPost]
public ActionResult Create(Model yourModel)
{
// your code goes here
}
in your view
@using(Html.BeginForm("HandleForm", "Home")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.