2012-07-25 53 views
1

以下是我的查看页面。没有<form>标记,当我插入值时,它完全插入。但是,当我插入<form>标记的值不插入时,调试点指向[HttpGet]而不是[HttpPost]。为了验证目的,我插入了<form>标签。但它似乎是没有用的..告诉我的解决方案....当我提交表单的值不插入剃须刀引擎mvc3?

@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();    
} 

回答

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> 

也请注意,我已经介绍URL帮手,而不是硬编码的网址给他们固定的脚本引用。

+0

然后如何在@using(Html.BeginForm())中使用id =“contact-form” – Sham 2012-07-25 15:55:21

+1

只需使用正确的重载:@using(Html.BeginForm(null,null,FormMethod.Post,new { id =“contact-form”}))'。看到我的答案。 – 2012-07-25 15:57:46

+0

现在值被插入到数据库....是的,我已经注意到,也....它的工作....你好! – Sham 2012-07-26 04:49:30