0

モデルにクラスがありますCustomer

Cshtmlページの送信ボタンをクリックすると、

@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)

    <fieldset>
        <legend>Registration</legend>
        <div class="editor-label">
            @Html.LabelFor(model => model.Fname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Fname)
            @Html.ValidationMessageFor(model => model.Fname)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Lname)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Lname)
            @Html.ValidationMessageFor(model => model.Lname)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Address)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Address)
            @Html.ValidationMessageFor(model => model.Address)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.phoneNo)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.phoneNo)
            @Html.ValidationMessageFor(model => model.phoneNo)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Username)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Username)
            @Html.ValidationMessageFor(model => model.Username)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Password)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Password)
            @Html.ValidationMessageFor(model => model.Password)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.ConfirmPassword)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ConfirmPassword)
            @Html.ValidationMessageFor(model => model.ConfirmPassword)
        </div>
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>

ページがコントローラーに投稿されない

[HttpPost]
public ViewResult DisplayCustomer(FormCollection Collection)
{
     objinfo.Fname = Request.Form["Fname"].ToString();
     objinfo.Lname = Request.Form["Lname"].ToString();
     objinfo.Address = Request.Form["Address"].ToString();
     objinfo.phoneNo = Request.Form["PhoneNo"].ToString();
     objinfo.Username = Request.Form["UserName"].ToString();
     objinfo.Password = Request.Form["Password"].ToString();
     objutility.InsertEmployee(objinfo);    

     return View("DisplayCustomer");
}

に値を取得できませんrequest.form。不足している特定のコードは何ですか?

4

1 に答える 1

1

1 - Request.Form または FormCollection を使用する必要はありません。

コントローラーは次のようになります。

// this method should just display the page, not handle the post back
public ViewResult DisplayCustomer(){
 return View(); 
}

// this method will handle the post back
[HttpPost]
public ViewResult DisplayCustomer(Customer model){
    // Handle the post back.

    if(ModelState.IsValid){
         /* Hanlde the submission, at this point "model" should have all the properties, such as model.Fname, model.Lname, etc... 

         DB.InserOnSubmit(model);
         DB.Customers.AddObject(model);
         DB.Customers.Add(model);
         you get the point 
         */
    }

}
于 2013-01-12T07:46:41.777 に答える