web-dev-qa-db-ja.com

送信ボタンのクリック時にビューからMVC4のコントローラーにテキストボックス値を取得する方法

ビューからテキストボックスの値をmvc4のコントローラに取得する方法コントローラでhttppostメソッドを使用すると、ページが見つかりませんというエラーが発生しました。

見る

@model MVC_2.Models.FormModel

@{
    ViewBag.Title = "DisplayForm";
}

@using (Html.BeginForm("DisplayForm", "FormController", FormMethod.Post))
{
    <form>
        <div>
            @Html.LabelFor(model => model.Empname)
            @Html.TextBoxFor(model => model.Empname)
           @* @Html.Hidden("Emplname", Model.Empname)*@

            @Html.LabelFor(model => model.EmpId)
            @Html.TextBoxFor(model => model.EmpId)
           @* @Html.Hidden("Emplid", Model.EmpId)*@

            @Html.LabelFor(model => model.EmpDepartment)
            @Html.TextBoxFor(model => model.EmpDepartment)
           @* @Html.Hidden("Empldepart", Model.EmpDepartment)*@

            <input type="button" id="submitId" value="submit" />

        </div>
    </form>
}

モデル

   public class FormModel
    {
        public string _EmpName;
        public string _EmpId;
        public string _EmpDepartment;

        public string Empname
        {
            get {return _EmpName; }
            set { _EmpName = value; }
        }

        public string EmpId
        {
            get { return _EmpId;}
            set {_EmpId =value;}
        }

        public string EmpDepartment
        {
            get { return _EmpDepartment; }
            set { _EmpDepartment = value; }
        }
    }

コントローラ

        public ActionResult DisplayForm()
        {
            FormModel frmmdl = new FormModel();
            frmmdl.Empname=**How to get the textbox value here from view on submitbutton click???**
        }
7
User

まず、ボタンの種類を"submit"に変更する必要があります。そのため、フォームの値はアクションメソッドに送信されます。

から:

<input type="button" id="submitId" value="submit" />

に:

<input type="submit" id="submitId" value="submit" />

次に、アクションメソッドのパラメーターとしてモデルを追加する必要があります。

[HttpPost]
public ActionResult DisplayForm(FormModel model)
    {
       var strname=model.Empname;
             return View();
    }

第三に、コントローラー名が「FormController」の場合。ビューのHtml.Beginformのパラメーターを次のように変更する必要があります。

@using (Html.BeginForm("DisplayForm", "Form", FormMethod.Post))

    {
    //your fields
    }

追伸ビューがActionメソッドである "DisplayForm"と同じ名前の場合、Html.BeginFormにパラメーターを追加する必要はありません。単純にするためです。そのようです:

@using (Html.BeginForm())
{
//your fields
}
12
Romeo

フォーム投稿用にActionResultを用意します。

[HttpPost]
public ActionResult DisplayForm(FormModel formModel)
{
    //do stuff with the formModel
    frmmdl.Empname = formModel.Empname;
}

Model Binding を調べます。デフォルトのモデルバインディングは、投稿されたフォーム値に埋め込まれたデータを取得し、それらからオブジェクトを作成します。

3
DGibbs

電子メールテキストボックスを使用して簡単なASP.NET MVCサブスクリプションフォームを実装しましょう。

モデル

フォームのデータはこのモデルにマッピングされます

public class SubscribeModel
{
    [Required]
    public string Email { get; set; }
}

見る

ビュー名はコントローラーメソッド名と一致する必要があります。

@model App.Models.SubscribeModel

@using (Html.BeginForm("Subscribe", "Home", FormMethod.Post))
{
    @Html.TextBoxFor(model => model.Email)
    @Html.ValidationMessageFor(model => model.Email)
    <button type="submit">Subscribe</button>
}

コントローラ

コントローラーは、要求の処理と適切な応答ビューの返送を担当します。

public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Subscribe(SubscribeModel model)
    {
        if (ModelState.IsValid)
        {
            //TODO: SubscribeUser(model.Email);
        }

        return View("Index", model);
    }
}

これが私のプロジェクト構造です。 「ホーム」ビューフォルダはHomeController名と一致することに注意してください。

ASP.NET MVC structure

2
Andrei

モデルはアクションのオブジェクトとして投稿され、次のように投稿時にアクションで取得できます。

[HttpPost]
public ActionResult DisplayForm(FormModel model)
        {
            // do whatever needed
          string emp = model.EmpName; 
        }

データを投稿する場合は、常にアクションにHttpPost属性を配置します。

ビューにも間違いがあります。次のようにします。

@using (Html.BeginForm("DisplayForm", "Form", FormMethod.Post))
{
        <div>
            @Html.LabelFor(model => model.Empname)
            @Html.TextBoxFor(model => model.Empname)
           @* @Html.Hidden("Emplname", Model.Empname)*@

            @Html.LabelFor(model => model.EmpId)
            @Html.TextBoxFor(model => model.EmpId)
           @* @Html.Hidden("Emplid", Model.EmpId)*@

            @Html.LabelFor(model => model.EmpDepartment)
            @Html.TextBoxFor(model => model.EmpDepartment)
           @* @Html.Hidden("Empldepart", Model.EmpDepartment)*@

            <input type="button" id="submitId" value="submit" />

        </div>

}
1
Ehsan Sajjad

隠しフィールドからのモデル値?以下に示す強く型付けされたアプローチをお勧めします。

public ActionResult DisplayForm(string Emplname, string Emplid, string Empldepart)
0
Paul Zahra

これを行うには2つの方法があります。

最初はTryUpdateModelを使用します:

    public ActionResult DisplayForm()
    {
        FormModel frmmdl = new FormModel();
        TryUpdateModel (frmmdl);

        // Your model should now be populated
    }

もう1つのより単純なバージョンは、アクションの[HttpPost]バージョンのパラメーターとしてモデルを保持することです。

    [HttpPost]
    public ActionResult DisplayForm(FormModel frmmdl)
    {
        // Your model should now be populated
    }
0
Adrian Wragg

以下のようにコントローラーを変更します。

[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
   var Empname = model.Empname;
}
0
Ashwini Verma
[HttpPost]
 public ActionResult DisplayForm(FormModel model)
 {
     FormModel frmmdl = new FormModel();
     frmmdl.Empname=**How to get the textbox value here from view on submitbutton //click???**
 }

model.Empnameには値があります

0
Zaid Bin Irfan

GetメソッドとPostメソッドの両方が必要です。

[HttpGet]
public ActionResult DisplayForm()
    {
       FormModel model=new FormModel();
             return View(model);
    }
[HttpPost]
public ActionResult DisplayForm(FormModel model)
    {
       var employeeName=model.Empname;
             return View();
    }
0
Ni3
[HttpPost]
public ActionResult DisplayForm(FormModel model)
{
    var value1 = model.EmpName;
}
0
VictorR