使用ASP MVC将span的数据绑定到数据库,使用Model Binder

时间:2016-07-05 07:04:38

标签: javascript jquery asp.net asp.net-mvc

我是Asp.net MVC的新手

我需要一些关于如何制作计数器的建议,然后将其值赋予模型装订器。

我有两个modal Pages.in第一个我有这个计数器供用户选择他们的请求数量,如下所示: 当用户点击加号图标时,号码已更改By Javascript

enter image description here

当用户点击下一个按钮时,下一个模态页面显示,我有selecteddatenumber of travelers javascript。还有一种形式,我希望模型粘合剂成为它的对象。

这是我在摘要中的下一个模态页面的视图

<p>
 <span  id="selecteddate"></span>


  <span id="selectedcount"></span>


  <span id="selectedprice"></span>
</p>

这里是javascript:

$(".nextbtn").click(function () {

$("#selecteddate").html("Selected Date is : "+$("#showdate").html());
$("#selectedprice").html("Total Price is : " + $("#tpriceforall").html());
$("#selectedcount").html($("#shownum").html());
 });

它正常工作。

我也在下一个模态页面中有一个Form。我想将用户的输入数据传递给数据库+ span #selectedcount java script set it each time中可用的数字。

因为我想使用ModelBinder,所以会隐藏一个输入。 但只是传递数据。这是我的请求模型的TravelersCount的输入

<div class="form-group">
@Html.LabelFor(model => model.Request.TravelersCount, htmlAttributes: new {@class = "control-label col-md-2" })
 <div class="col-md-10">
   @Html.EditorFor(model => model.Request.TravelersCount, new { htmlAttributes = new { @class = "form-control", @*@Value=?????*@ } })
@Html.ValidationMessageFor(model => model.Request.TravelersCount, "", new { @class = "text-danger" })
 </div>
</div>

我应该在Value中为EditorFor撰写什么?

或者,如果这种方式完全不正常,请建议我新的。 真的很感激你的帮助。感谢

1 个答案:

答案 0 :(得分:1)

您的EditorFor()方法使用id="Request_TravelersCount"创建输入。

要在单击按钮时更新其value,您可以使用

$(".nextbtn").click(function () {
    $("#selecteddate").html("Selected Date is : "+$("#showdate").html());
    $("#selectedprice").html("Total Price is : " + $("#tpriceforall").html());
    var num = $("#shownum").html();
    $("#selectedcount").html(num);
    $('#Request_TravelersCount').val(num);
});

附注:您不应尝试在HtmlHelper方法中设置值属性(该方法会设置正确的值并考虑ModelState值)

相关问题