1

下面是我的查看页面。<form>当我插入值时没有标签,它完美地插入。但是当我用<form>标签插入未插入的值时,调试点指向[HttpGet]而不是[HttpPost]. 出于验证目的,我已插入<form>标签。但这似乎没有用..告诉我解决方案....

@model CustMaster.Models.MyAccount
@using CustMaster.Models

@{
    ViewBag.Title = "Customer Information";
}    

<html>
<head>
   <title>Customer Information</title>
   <link href="../../style.css" rel="stylesheet">     
</head>
<body>

<form action="" id="contact-form" class="form-horizontal">

   @using (Html.BeginForm())
   {        
      <table>
        <tr>
           <td><h3>User Information</h3></td>
        </tr>
      </table>
      <table>
         <tr>
            <td>
             <div class="control-group">

               @Html.LabelFor(model => model.CustomerFirstName, 
               new { @class = "control-label" })

                <div class="controls">
                   @Html.TextBoxFor(model => model.CustomerFirstName, 
                   new { @class = "input-xlarge" })

                </div> 
             </div>
           </td>

          <td>
             <div class="control-group"> 

               @Html.LabelFor(model => model.CustomerMiddleName, 
               new { @class = "control-label" })

                <div class="controls">
                     @Html.TextBoxFor(model => model.CustomerMiddleName, 
                     new { @class = "input-xlarge" })
                </div>
             </div>
         </td>
      </tr>     
 </table>
 <button type="submit">Register</button>
}
@*</form>*@

   <script type="text/javascript" src="../../assets/js/jquery-1.7.1.min.js"></script>   
   <script type="text/javascript" src="../../assets/js/jquery.validate.js"></script>   
   <script type="text/javascript" src="../../assets/js/jquery.validate.min.js"></script>   
   <script type="text/javascript" src="../../script.js"></script>   

</body>
</html>

下面是我的控制器

[HttpGet]
public ActionResult Index()
{
   return View();
}

[HttpPost]
public ActionResult Index(MyAccount myacc)
{
   MyAccount ma = new MyAccount();
   var objview = ma.GetCustInfo(myacc);
   return View();            
}
4

1 回答 1

1

您不能嵌套 html 表单 => 它会导致无效标记和未定义行为。您将不得不删除外部<form>. Html.BeginForm已经生成了一个表单标签。您似乎已经评论了结束表单标签,但没有评论开头。

请稍微格式化您的代码。读起来完全一团糟:

@model CustMaster.Models.MyAccount
@using CustMaster.Models

@{
    ViewBag.Title = "Customer Information";
}

<html>
<head>
    <title>Customer Information</title>
    <link href="@Url.Content("~/style.css")" rel="stylesheet">
</head>
<body>
    @using (Html.BeginForm(null, null, FormMethod.Post, new { id = "contact-form" })) 
    {
        <table>
            <tr>
                <td><h3>User Information</h3></td>
            </tr>
        </table>
        <table>
            <tr>
                <td>
                    <div class="control-group">
                       @Html.LabelFor(model => model.CustomerFirstName, new { @class = "control-label" })
                       <div class="controls">
                           @Html.TextBoxFor(model => model.CustomerFirstName, new { @class = "input-xlarge" })
                       </div> 
                   </div>
               </td>
               <td>
                   <div class="control-group">
                       @Html.LabelFor(model => model.CustomerMiddleName, new { @class = "control-label" })
                       <div class="controls">
                           @Html.TextBoxFor(model => model.CustomerMiddleName, new { @class = "input-xlarge" })
                       </div>
                   </div>
               </td>
           </tr>
       </table>

       <button type="submit">Register</button>
    }

    <script type="text/javascript" src="@Url.Content("~/assets/js/jquery-1.7.1.min.js")"></script>   
    <script type="text/javascript" src="@Url.Content("~/assets/js/jquery.validate.js")"></script>   
    <script type="text/javascript" src="@Url.Content("~/assets/js/jquery.validate.min.js")"></script>   
    <script type="text/javascript" src="@Url.Content("~/script.js")"></script>
</body>
</html>

Also please notice that I have fixed your script references by introducing url helpers instead of hardcoding the url to them.

于 2012-07-25T15:53:31.873 回答