1

希望有好心帮我,这可能是一个简单的问题控制器,但我感到沮丧,因为没有为它的修复,如何发送隐藏字段,通过模型绑定

我想从一个隐藏字段传递值通过模型绑定到控制器, 无论什么方法,我使用,只需从视图中传递值通过模型控制器能为我工作,

这是我的滑块范围

<label for="amount">دامنه قیمت:</label> 
 
<div id="connect" class="noUi-target noUi-ltr noUi-horizontal noUi-background"> 
 

 
</div>

这是隐藏字段,

<input type="hidden" name="MinPrice" value="@Model.MinPrice" id="MinPrice" /> 
 
<input type="hidden" name="MaxPrice" value="@Model.MaxPrice" id="MaxPrice" />

<script> 
 
    var connectSlider = document.getElementById('connect'); 
 

 
noUiSlider.create(connectSlider, { 
 
    start: [40000, 800000], 
 
    connect: true, 
 
    range: { 
 
    'min': 0, 
 
    'max': 2000000 
 
    } 
 
}); < /script> 
 
     <script> 
 
      var connectBar = document.createElement('div'), 
 
       connectBase = connectSlider.querySelector('.noUi-base'); 
 

 
      // Give the bar a class 
 
for styling and add it to the slider. 
 
connectBar.className += 'connect'; 
 
connectBase.appendChild(connectBar); 
 

 
connectSlider.noUiSlider.on('update', function(values, handle, a, b, handlePositions) { 
 

 
    var offset = handlePositions[handle]; 
 

 
    // Right offset is 100% - left offset 
 
    if (handle === 1) { 
 
    offset = 100 - offset; 
 
    } 
 

 
    // Pick left for the first handle, right for the second. 
 
    connectBar.style[handle ? 'right' : 'left'] = offset + '%'; 
 
}); < /script> 
 
     
 

 
     <script> 
 

 

 
      var valueInput = document.getElementById('MinPrice'), 
 
       valueSpan = document.getElementById('MaxPrice'); 
 

 
      // When the slider value changes, update the input and span 
 
connectSlider.noUiSlider.on('update', function(values, handle) { 
 
    if (handle) { 
 
    valueInput.value = values[handle]; 
 
    } else { 
 
    valueSpan.innerHTML = values[handle]; 
 
    } 
 
}); 
 

 
// When the input changes, set the slider value 
 
valueInput.addEventListener('change', function() { 
 
    connectSlider.noUiSlider.set([null, this.value]); 
 
}); 
 

 

 
< /script>

这是我的JS,填补了隐藏字段,这两个字段填写正确,但不要通过型号向控制器发送数据,

感谢各位朋友,

更新

这是我的模型:

public class SearchModel 
{ 
    public string Devision { get; set; } 
    public string Gender { get; set; } 
    public int? MinPrice { get; set; } 
    public int? MaxPrice { get; set; } 
    public string Brand { get; set; } 
    public string Sport { get; set; } 
    public string Size { get; set; } 
    public string ProductGroup { get; set; } 
    public List<tblSiteItem> Result { get; set; } 
    public List<tblSiteItem> Sales { get; set; } 
    public List<string> Devisions { get; set; } 
    public List<string> Genders { get; set; } 
    public List<string> Brands { get; set; } 
    public List<string> Sports { get; set; } 
    public List<string> Sizes { get; set; } 
    public List<string> ProductGroups { get; set; } 
    public List<tblSiteCart> CartItems { get; set; } 
    public int PageNo { get; set; } 
    public int Barcode { get; set; } 
} 

正如我所说的值是否正确填写,所以我不需要代码,使其更正,只是不将hdnValue绑定到我的模型中的字段。 我的意思是MinPrice和MaxPrice。

编辑 我不能悲伤地上传图片,如果我的一切是相当明显的看, 什么办法,我解释一下细节:

  1. 当我改变了slideBar,我的JS的火灾和值的变化,

  2. 当我点击搜索按钮我的模型中的所有领域是除了这两个领域的领域,我的意思是MinPrice和MaxPrice

  3. 结果显示空白页,又是我的js不断射击,然后回来为默认值,

在最后一次尝试

我改变了隐藏字段类型=“文本” 和锯当我更改滑动条时,该值在我的文本框中,但在触摸搜索按钮时未绑定到我的视图模型。

我肯定有什么毛病我的视图模型,但我怎么能明白什么是不正确:(请您看看我的代码

回答

0

使用此代码获得的价值:

var userRole = ""; 
 
userRole = $("#hdnVal").val(); 
 
alert(userRole);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<input type="hidden" id="hdnVal" value="@Model.MinPrice" />

+0

在你的榜样了你编写你的价值我希望成为一个Model.MinPrice,与我希望看到我的模型充满了这个在控制器 – sariiia

+0

**数据试试这个:** 而不是价值=“YourValue”你可以把值=“@ Model.MinPrice”。 –

+0

我测试了这个太多,但没有成功,请查看有一个字段必须更新的帖子,感谢您的时间我的朋友 – sariiia

0

关注这个。

在.cshtml页

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="hidden" id="hdnVal" value="@Model.MinPrice" /> <input type="button" name="Submit" value="Submit" id="btnSubmit" class="btn btn-primary" />

的js

var userRole = ""; 
userRole = $("#hdnVal").val(); 
alert(userRole); 
$('#btnSubmit').click(function() { 
     var formData = new FormData(); 
     formData.append("userRole ", userRole); 
     $.ajax({ 
     type: "POST", 
     url: "/Controller/YourAction/", 
     data: formData, 
     dataType: 'json', 
     contentType: false, 
     processData: false, 
     success: function(result) { 
      if (result.Message == "success") { 
      alert("Success"); 
      } 
     } 
     }); 

    } 

C#代码

public JsonResult YourAction() 
{ 
string Role = Request.Form["userRole "]; 
//You can get the value of role 
} 
+0

请看更新的部分,我并不需要有一个hdnField控制器,而不是一个模型被填充hdnField,model.Minprice = hdnValue – sariiia

0

您输入的名称不正确。您应该从中删除Model.部分。如果您使用HtmlHelper.HiddenFor,你会得到如下:

<input type="hidden" name="MaxPrice" value="@Model.MaxPrice" id="MaxPrice" /> 

这样,输入将被正确提交您的模型。

看一看this answer

+0

它不工作,我不知道最新的问题,我有其他领域,你可以在我的模型中看到他们的工作,但这个隐藏的... – sariiia

+0

我也曾经使用过这样的:@ Html.HiddenFor(M => m.MinPrice,新{ID = “MinPrice”,值= @ Model.MinPrice}),但它再次DES不与hdnValue, – sariiia

+0

这真是奇怪填写MinPrice是..在提交表单之前,您确定这些值是否正确更新? – Dygestor

相关问题