将更新的只读值更新为MVC中的Action

时间:2016-01-14 17:48:40

标签: c# jquery asp.net-mvc nouislider

我的form低于method=get

@using (Html.BeginForm("Search", "Home", FormMethod.Get, htmlAttributes: new { @class = "main-search", id = "frmsearch", role = "form" })) {
<div class="row">
  <div class="col-md-3 col-sm-3">
    <div class="form-group">
      <label for="type">Property Type</label>
      @Html.ListBoxFor(m => m.searchModel.CategoriesId, Model.searchModel.Categories, htmlAttributes: new { id = "type", multiple = "multiple", @class = "animate", data_transition_parent = ".dropdown-menu", title = "All" })
    </div>
    <!-- /.form-group -->
  </div>

  <div class="col-md-3 col-sm-3">
    <div class="form-group">
      <label for="location">Location</label>
      @Html.DropDownListFor(m => m.searchModel.LocationID, Model.searchModel.Locations, htmlAttributes: new { id = "location", multiple = "multiple", @class = "animate", data_transition_parent = ".dropdown-menu", title = "All" })
    </div>
    <!-- /.form-group -->
  </div>
  <div class="col-md-3 col-sm-3">
    <div class="form-group">
      <label>Status</label>
      @Html.DropDownListFor(m => m.searchModel.StatusID, Model.searchModel.Status, htmlAttributes: new { id = "status", multiple = "multiple", @class = "animate", data_transition_parent = ".dropdown-menu", title = "All" })
    </div>
    <!-- /.form-group -->
  </div>
  <div class="col-md-2 col-sm-3">
    <div class="form-group">
      <label>Price</label>
      <div class="ui-slider" id="price-slider-category" data-value-min="@Model.searchModel.MinPrice" data-value-max="@Model.searchModel.MaxPrice" data-value-type="price" data-currency="&#8377;" data-currency-placement="before">
        <div class="values clearfix">
          @Html.TextBoxFor(m => m.searchModel.MinPrice, htmlAttributes: new { id = "value-min", @class = "value-min", name = "value-min[]", @readonly = "readonly", style = "padding:0px" }) 
          @Html.TextBoxFor(m => m.searchModel.MaxPrice, htmlAttributes: new { id = "value-max", @class = "value-max", name = "value-max[]", @readonly = "readonly", style = "padding:0px" })
        </div>
        <div class="element"></div>
      </div>
    </div>
    <!-- /.form-group -->
  </div>
  <div class="col-md-1 col-sm-3">
    <div class="form-group">
      <label></label>
      <button type="submit" style="color:white" id="searchSubmit" class="btn btn-block blue waves-effect">
        <i class="fa fa-search"> </i>
      </button>
    </div>
    <!-- /.form-group -->
  </div>
  <!--/.col-md-6-->
</div>
<!--/.row-->
}

我有JS通过form

发布AJAX个值
$(document).on('click', '#searchSubmit', function (e) {
        var _form = $(this).closest('form');
        var _url = _form.attr('action');
        var formData = _form.serialize();
        var request = $.get(_url, formData);
        request.complete(function (response) {

        })
})

这是我的model

public class SearchFilters
{
    public SearchFilters()
    {
         MinPrice = 10000;
         MaxPrice=8000000;
    }
    public IEnumerable<SelectListItem> Categories { get; set; }
    public int[] CategoriesId { get; set; }

    public IEnumerable<SelectListItem> Locations { get; set; }
    public int[] LocationID { get; set; }

    public IEnumerable<SelectListItem> Status { get; set; }
    public int[] StatusID { get; set; }

    public int MinPrice { get; set; }
    public int MaxPrice { get; set; }
}

这是我处理搜索请求的controller方法。

[HttpGet]
public ActionResult Search([Bind(Prefix = "searchModel")]SearchFilters smodel)
{
     ProjectsViewModel model = new ProjectsViewModel();
     //search db and fill model
     return PartialView("_PropertyDetails", model);
}

使用noUiSlider插件对最小值和最大值进行UI渲染,因此输入为readonly,但会通过update noUiSlider选项进行更新。但是,只要在Server中收到模型,它就会作为默认值分配给模型变量,即使在更新之后也是如此在DOM中检查时,这些值不会更新,但会反映在UI中。是的,这是因为readonly的{​​{1}}属性,但是否还有其他方法可以在这些类型的情况下发布只读属性值?以下是{{}的几个屏幕截图收到后,我会查看{} textboxUI值。

UI

enter image description here

DOM

enter image description here

模型

enter image description here

更新

我可以在DOM中看到model中的发布值,但不会在模型中看到。不知道怎么做到这个..

1 个答案:

答案 0 :(得分:2)

,格式设置将MinPriceMaxPrice作为字符串。因此,这些不会绑定到int属性。只需删除格式并在GET中发送它们,然后它们就会绑定到int属性。