发布部分表格Asp.net mvc

时间:2015-02-27 19:11:45

标签: jquery asp.net-mvc textbox html.dropdownlistfor

我有一个包含两个文本框,两个下拉列表和一个标签的表单。当我填充前两个文本框并选择两个下拉列表的值时,所有值将被连接并放入标签中。所以现在当我点击按钮保存时,我想只发布标签的值,而不是这些其他文本框和清单。如何激活标签的post方法并禁用其他方法? 以下是我目前的代码:

   <div class="panel-body">
            @using (Html.BeginForm())
            {
                <div class="form-group">
                    <div class="row">
                        <div class="col-md-6">

                            @Html.Label("Part/Location", new {@class = "control-label"})
                            @Html.TextBox("PartLocation", null, new { @class = "form-control" })
                            @Html.ValidationMessageFor(model => model.Name)

                        </div>
                        <div class="col-md-6">

                            @Html.Label("Index", new {@class = "control-label"})
                            @Html.TextBox("Index", null , new {@class = "form-control"})
                            @Html.ValidationMessageFor(model => model.Name)

                        </div>

                    </div>

                    <div class="row">
                        <div class="col-md-6">

                            @Html.Label("Measurement", new {@class = "control-label"})
                            @Html.DropDownList("Measurements", "Select measurement")
                            @Html.ValidationMessageFor(model => model.ChannelGroupId)

                        </div>
                        <div class="col-md-6">

                            @Html.Label("Location", new {@class = "control-label"})
                            @Html.DropDownList("DirectionTypes","Select direction")
                            @Html.ValidationMessageFor(model => model.ChannelGroupId)

                        </div>
                    </div>

                    <div class="row">
                        <div class="col-md-6">

                            @Html.LabelFor(model => model.ChannelGroupId, new {@class = "control-label"})
                            @Html.DropDownListFor(x => x.ChannelGroupId, Model.ChannelGroups, "Select Channel Group", new {@class = "form-control"})
                            @Html.ValidationMessageFor(model => model.ChannelGroupId)

                        </div>
                        <div class="col-md-6">
                            <label class="control-label"></label>
                            <a href="#" id="addChannelGroup" class="form-control" style="border: none">
                                <i class="fa fa-plus-circle">Add Group</i>
                            </a>
                        </div>
                    </div>
                    <br />
                    <div class="row">
                        <div class="col-md-6">
                            @Html.Label("Channel name: ", new { id = "channelName",@class = "control-label" })
                        </div>
                    </div>

                </div>

这是Jquery处理连接:

 $("#PartLocation, #Index,#Measurements,#DirectionTypes").change(function () {
        var partLocationText = $("#PartLocation").val();
        var indexText = $("#Index").val();
        var mesurementSelected = $("#Measurements option:selected").text();
        var directionSelected = $("#DirectionTypes option:selected").text();

            $("#channelName").empty();
            $("#channelName").append("Channel name: " + partLocationText + "_" + indexText);

        if (mesurementSelected != "Select measurement") {
            $("#channelName").append("_" + mesurementSelected);
        }
        if (directionSelected != "Select direction") {
            $("#channelName").append("_" + directionSelected);
        }
    });

1 个答案:

答案 0 :(得分:2)

你可以在这里做很多事情来达到你想要的目的。

1.根据HTML规范,如果没有名称属性,所有HTML控件(输入,选择)都不会发布。如此简单从这些控件中删除名称属性将阻止它们发布其值。除此之外,由于标签值未发回,您还必须创建隐藏文本框,将其值更新为连接值(即标签的值),然后允许发布。

$(function(){
 $("#PartLocation, #Index,#Measurements,#DirectionTypes").removeAttr('name');
 //this will remove their name attribute and hence their values wont be posted back
})

并更新隐藏文本框的值(输入[type =&#34; text&#34;])。

$("#PartLocation, #Index,#Measurements,#DirectionTypes").change(function () {
       //your code
       //let cs be the final concatenated string.
       $("#channelName").append(cs);
       $('#hiddenInput').val(cs);
    });


2.您可以通过Javascript接管发布机制,仅发布您要发布的值。显然,为了实现这一点,您最好将操作方法​​的返回类型更改为HttpResultJsonResult,以更适合您的方式。

$(function(){
$('form').submit(function(f, e){
  e.preventDefault();
  //callculate your concatenated string.
  //assumed to be cs
  $.ajax(url/*url to ur post action method*/, {
   //fill in the suitable params see the jquery link for more help
  })
})
})

JQUERY AJAX LINK