2017-06-19 82 views
0

在我提交更新表格之前,我想檢查一下Status值是否不小於ziro(在ziro下) 我的意思是這部分表格。如何從Asp.Net Mvc表格中獲取價值

<div class="form-group"> 
      @Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Status, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

我如何,如果我用這個JavaScript來發送孔形式得到這個特定的值:

$(document).ready(function() { 


     $("#btnUpdateProduct").click(function() { 

      // Some where here or after var myformdata = $("#myForm").serialize(); I must check if 
      // Value of @Html.LabelFor(model => model.Status ..... 

      var myformdata = $("#myForm").serialize(); 

      $.ajax({ 

       type: "POST", 
       url: "/Home/EditProduct", 
       data: myformdata, 
       success: function() { 

        $("#updateModal").modal("hide"); 
        location.reload(); 

       } 

      }) 
     }) 

    }) 

這是我的洞觀形成..

@model Products.Models.Product 

<div> 
    <form id="myForm"> 
    <div class="form-horizontal"> 
     <h4>Product</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     @Html.HiddenFor(model => model.ProductId) 
     @Html.HiddenFor(model => model.CountryId) 
    @Html.HiddenFor(model => model.ModelId) 


     <div class="form-group"> 
      @Html.LabelFor(model => model.ProductName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
      @Html.EditorFor(model => model.ProductName, new { htmlAttributes = new { @class = "form-control" } }) 
      @Html.ValidationMessageFor(model => model.Model, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Model, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Model, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Model, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Status, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Status, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Save" class="btn btn-default" id="btnUpdateProduct"/> 
      </div> 
     </div> 
    </div> 

謝謝!

+0

如果您嘗試使用$(「#Status」).val()來獲取輸入字段值,該怎麼辦? – Shyju

+0

@Shyju謝謝你的迴應,我試過之前,但它給了我空的 –

回答

0

如果您給編輯器一個id,以下內容可以用於驗證您的狀態。

剃刀變化。

@Html.EditorFor(model => model.Status, new { htmlAttributes = new { @class = "form-control", @id = "MyStatus" } }) 

然後這裏是你的JavaScript你可以嘗試。

var status = document.getElementById("MyStatus").value; 
if(status >= 0) 
{ 
    $.ajax({ 
       type: "POST", 
       url: "/Home/EditProduct", 
       data: myformdata, 
       success: function() { 

        $("#updateModal").modal("hide"); 
        location.reload(); 

       } 

      }) 
} 
else 
    alert("Status is not greater than zero"); 
+0

完美,現在工作。非常感謝你! –

0

我會阻止用戶甚至擊中提交,直到符合條件。猜猜只是個人喜好。

@Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2 checkMe" }) 

$(document).ready(function() { 
    $('#btnUpdateProduct').attr('disabled', 'disabled'); 
    $('.checkMe').change(function(){ 
     if(parseInt($(this).val()) > 0) { 
     $('#btnUpdateProduct').removeAttr('disabled'); 
     } else { 
     $('#btnUpdateProduct').attr('disabled', 'disabled'); 
     } 
    }); 

    $("#btnUpdateProduct").click(function() { 

     // Some where here or after var myformdata = $("#myForm").serialize(); I must check if 
     // Value of @Html.LabelFor(model => model.Status ..... 

     var myformdata = $("#myForm").serialize(); 

     $.ajax({ 

      type: "POST", 
      url: "/Home/EditProduct", 
      data: myformdata, 
      success: function() { 

       $("#updateModal").modal("hide"); 
       location.reload(); 

      } 

     }) 
    }) 
})