2017-02-21 10 views
0

コントローラー発射ません:MVCアクション

public ActionResult Insert() 
{ 
    return View(); 
} 
public ActionResult Insert(Employee emp) 
{ 
    Employee emp1 = new Employee(); 
    emp1.insert(emp); 
    return View(); 
} 

CSHTML

@using (Html.BeginForm("Employee", "Insert", FormMethod.Post)) 
{ 
    @Html.TextBoxFor(model => model.name) 
    @Html.TextBoxFor(model => model.Email) 
    @Html.TextBoxFor(model => model.mob) 

    <input type="button" value="Register me" /> 
} 

を私は( '私を登録')ボタンをクリックしたときに、私のモデル値を保存する必要があります。前もって感謝します。

+1

それは 'する必要があります' <ボタンタイプを= ... "提出">(と私はあなたの第二の方法は、 '[HttpPost]'持っていると仮定) –

答えて

7

は、コントローラの属性を設定してください:

[HttpGet] // here ([HttpGet] is default so here you can delete this attribute) 
public ActionResult Insert() 
{ 
    return View(); 
} 

[HttpPost] // here 
public ActionResult Insert(Employee emp) 
{ 
    Employee emp1 = new Employee(); 
    emp1.insert(emp); 
    return View(); 
} 

あなたがフォームを送信する必要があるいくつかのアクションを呼び出すには。また、

<input type="submit" value="Register me" /> // type="button" -> type="submit" 

BeginFormであなたは、まずアクション名、その後、コントローラ名を指定する必要があります:あなたのbuttonsubmit種類を変更し

@using (Html.BeginForm("Insert", "Employee", FormMethod.Post)) 
+2

また、Html.BeginForm( "** Insert **"、 "Employee"、FormMethod.Post)? – Amit

+0

@Amit、thanks、編集済み –

3

そのあなたはINSERT action

[HttpPost] 
    public ActionResult Insert(Employee emp) 
    { 
     Employee emp1 = new Employee(); 
     emp1.insert(emp); 
     return View(); 
    } 
HTTP POSTを宣言していないので

Beginformとを使用している場合210はPostです。関連するアクションは同じ種類のHTTPを持つ必要があります。最初は[HttpGet]があるかどうかは関係ありません。ActionResult MVCでは、どのタイプのHTTPリクエスト/応答も宣言していないActionResultは[HttpGet]あなたのBeginFormでも

():

@using (Html.BeginForm("ActionName(Insert)", "ControllerName", FormMethod.Post)) 
{ 
    @Html.TextBoxFor(model => model.name) 
    @Html.TextBoxFor(model => model.Email) 
    @Html.TextBoxFor(model => model.mob) 

    <input type="submit" value="Register me" /> 
} 
関連する問題