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 回答