Javascript 如何从Asp.NETMVC表单中获取价值
在提交更新表单之前,我想检查状态值是否不小于ziro(ziro以下) 我是指表格的这一部分Javascript 如何从Asp.NETMVC表单中获取价值,javascript,asp.net,asp.net-mvc,Javascript,Asp.net,Asp.net Mvc,在提交更新表单之前,我想检查状态值是否不小于ziro(ziro以下) 我是指表格的这一部分 <div class="form-group"> @Html.LabelFor(model => model.Status, htmlAttributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Ht
<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>
这是我的孔视图表
@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>
@model Products.Models.Product
产品
@Html.ValidationSummary(true,“,new{@class=“text danger”})
@Html.HiddenFor(model=>model.ProductId)
@Html.HiddenFor(model=>model.CountryId)
@Html.HiddenFor(model=>model.ModelId)
@LabelFor(model=>model.ProductName,htmlAttributes:new{@class=“control label col-md-2”})
@EditorFor(model=>model.ProductName,new{htmlAttributes=new{@class=“form control”}})
@Html.ValidationMessageFor(model=>model.model,“,new{@class=“text danger”})
@LabelFor(model=>model.model,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.model,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.model,“,new{@class=“text danger”})
@LabelFor(model=>model.Status,htmlAttributes:new{@class=“controllabel col-md-2”})
@EditorFor(model=>model.Status,new{htmlAttributes=new{@class=“form control”})
@Html.ValidationMessageFor(model=>model.Status,“,new{@class=“text danger”})
谢谢大家! 如果您给编辑器一个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");
我甚至会阻止用户点击submit,直到满足您的条件。我猜只是个人喜好
@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();
}
})
})
})
如果您尝试
$(“#Status”).val()
获取输入字段值,会怎么样?@Shyju谢谢您的回复,我以前尝试过,但结果为空
@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();
}
})
})
})