如何从Asp.Net Mvc表中获取价值

时间:2017-06-19 17:54:25

标签: 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">
                @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>

谢谢!

2 个答案:

答案 0 :(得分:0)

如果您为编辑提供了ID,则以下内容可用于验证您的状态。

Razor Change。

 @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");

答案 1 :(得分:0)

我阻止用户甚至点击提交,直到满足您的条件。猜猜只是个人偏好。

$content = [IO.File]::ReadAllText("C:\Path\Input.txt") -replace "`r`n","`n"
[IO.File]::WriteAllText("C:\Path\Output.txt", $content)