简体   繁体   English

使用ASP MVC将范围数据绑定到数据库,使用Model Binder

[英]Bind Data Of a span to database with ASP MVC,Uses Model Binder

I'm new in Asp.net MVC 我是Asp.net MVC的新手

I need some suggestions for how to make a counter then give the value of it to model binder. 我需要一些建议,以了解如何制作一个计数器,然后将其值提供给粘结剂模型。

I have two modal Pages.in the first one I have this counter for users to choose the number of their request it's like this : when user click on plus icon the number changed By Javascript 我有两个modal页面。在第一个页面中,我有一个计数器供用户选择其请求编号,如下所示:当用户单击加号图标时,编号By Javascript更改

在此处输入图片说明

when user click on next button , the next modal page shows, which I have selecteddate and number of travelers there by javascript. 当用户单击next按钮时,将显示下一个模式页面,我已在其中selecteddate和使用javascript number of travelers and also a form which I want model binder to make an object of it. 以及我希望模型活页夹成为其对象的形式。

here is the view of my next modal page in summary 这是我的下一个模态页面的摘要

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


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


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

and here is javascript : 这是javascript:

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

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

and it works correctly. 它可以正常工作。

and also I have a Form in this next modal page too . 在下一个模态页面中我也有一个表单。 I want to pass input data of users to data base + the number which is available in span #selectedcount that java script set it each time . 我想将用户的输入数据传递给数据库+ java script set it each time span #selectedcount中可用的数字。

as I want to use ModelBinder , there is an input which it would be hidden. 正如我想使用ModelBinder ,有一个输入将被隐藏。 but just to pass data . 只是为了传递数据。 here is the input for TravelersCount of my Request Model 这是我的请求模型的TravellersCount的输入

<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>

What Should I write for Value in the EditorFor ? 我应该在EditorForValue写什么?

Or if this way is completely irregular please suggest me new one. 或者,如果这种方式是完全不规则的,请给我建议一个新方法。 Really appreciate you'r help. 非常感谢您的帮助。 thanks 谢谢

Your EditorFor() method creates an input with id="Request_TravelersCount" . 您的EditorFor()方法创建一个带有id="Request_TravelersCount"的输入。

To update its value when the button is clicked, you can use 要在单击按钮时更新其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);
});

Side note: You should not attempt to set the value attribute in a HtmlHelper method (the method will set the correct value taking into account ModelState values) 旁注:您不应尝试在HtmlHelper方法中设置value属性(该方法将考虑ModelState值来设置正确的值)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM