【问题标题】:Html page doesn't post to a controllerHtml 页面不会发布到控制器
【发布时间】:2012-12-26 19:31:53
【问题描述】:

我在模特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。我缺少什么特定代码?

【问题讨论】:

  • 我猜@using(html.BeginForm()){} 仍然缺少结尾'}'

标签: asp.net asp.net-mvc asp.net-mvc-3 visual-studio-2010


【解决方案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 
         */
    }

}

【讨论】:

  • 谢谢先生,它让我免于编写大量代码。
猜你喜欢
  • 1970-01-01
  • 2012-01-19
  • 2013-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-02
相关资源
最近更新 更多