2012-07-25 52 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