如何确定MVC上的确切httppost动作?

时间:2016-01-14 09:35:47

标签: asp.net-mvc autopostback

这是我的行动方法

[HttpGet]
 public ActionResult Index()
        {
           ViewBag.WASELATSAFAR = rep.Get_WASELATSAFAR();
           string EntedabiD = rep.GetID();
           ViewData["ID"] = EntedabiD;
            return View();
        }

在Entedab控制器内部

Html

 <div class="form-actions fluid" style="    margin-top: 87px;">
                        <div class="portlet-body">
                            <div class="table-toolbar">
                                <div class="btn-group">
                                    <a id="AddEmployee" class="btn green">
                                        <span style="font-size: 18px !important; font-family: -webkit-body; "> اضافة موظف</span>
                                        <i class="fa fa-plus"></i>
                                    </a>
                                </div>
                            </div>
                            <table class="table table-striped table-bordered table-hover" id="AllEmployees">
                                <thead>
                                    <tr>
                                        <th>
                                            serial
                                        </th>
                                        <th style="width: 200px;">
                                            Name
                                        </th>
                                        <th>
                                            Fia
                                        </th>
                                        <th>
                                            Degree
                                        </th>
                                        <th>
                                            Salary
                                        </th>
                                        <th>
                                            Transportation
                                        </th>

                                        <th>
                                        </th>
                                    </tr>
                                </thead>
                                <tbody id="AddedEmployees">
                                    @Html.Partial("_Employees")
                                </tbody>
                            </table>
                        </div>
                    </div>

我尝试用@using(Html.BeginForm())包围Html 像那样

@using (Html.BeginForm())
{

     <div class="form-actions fluid" style="    margin-top: 87px;">
                            <div class="portlet-body">
                                <div class="table-toolbar">
                                    <div class="btn-group">
                                        <a id="AddEmployee" class="btn green">
                                            <span style="font-size: 18px !important; font-family: -webkit-body; "> Add Emp</span>
                                            <i class="fa fa-plus"></i>
                                        </a>
                                    </div>
                                </div>
                                <table class="table table-striped table-bordered table-hover" id="AllEmployees">
                                    <thead>
                                        <tr>
                                            <th>
                                                serial
                                            </th>
                                            <th style="width: 200px;">
                                                Name
                                            </th>
                                            <th>
                                                Fia
                                            </th>
                                            <th>
                                                Degree
                                            </th>
                                            <th>
                                                Salary
                                            </th>
                                            <th>
                                               Transportation
                                            </th>

                                            <th>
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody id="AddedEmployees">
                                        @Html.Partial("_Employees")
                                    </tbody>
                                </table>
                            </div>
                        </div>
}

问题是,当我点击这个提交输入时,它转到了[HttpGet]动作方法,但我希望它转到这个

  [HttpPost]
  public ActionResult Entedab_Index(EntedabViewModel entedabObj)
        {

            if (ModelState.IsValid)
            {
                rep.add(entedabObj);
            }
            return RedirectToAction("Index");

        }

输入提交按钮

 <input id="submit" type="submit" value="Save" class="btn green btn-lg" 
                                   style=" float: left; font-family: -webkit-body; font-size: 20px; width: 200px;">

我正在尝试 -

    [HttpGet]
        public ActionResult Entedab_Index(){
        ViewBag.WASELATSAFAR = rep.Get_WASELATSAFAR();
         string EntedabiD = rep.GetID();
         ViewData["ID"] = EntedabiD;
         return View();
        } 


    [HttpPost]
    public ActionResult Entedab_Index(EntedabViewModel entedabObj)
     {
     if (ModelState.IsValid)
     {
    rep.add(entedabObj); 
    } 
    return RedirectToAction("Entedab_Index");
    }




@using (Html.BeginForm("Entedab_Index", "Entedab",FormMethod.Post)) {//html} 

但是会出现此错误&#34;已经添加了具有相同密钥的项目&#34;

1 个答案:

答案 0 :(得分:0)

您需要更改 -

 [HttpPost]
  public ActionResult Entedab_Index(EntedabViewModel entedabObj)

 [HttpPost]
  public ActionResult Index(EntedabViewModel entedabObj)

然后它将发布到当前请求的post方法。

另一种方法是给定义的url as-

@using (Html.BeginForm("Action","Controller",HttpMethod.Post))
{
}