neda Derakhshesh neda Derakhshesh - 5 months ago 14
jQuery Question

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

I'm new in 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


enter image description here

when user click on next button , the next modal page shows, which I have
selecteddate
and
number of travelers
there by javascript. 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 :

$(".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
.

as I want to use
ModelBinder
, there is an input which it would be hidden.
but just to pass data . here is the input for TravelersCount of my Request Model

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

Or if this way is completely irregular please suggest me new one.
Really appreciate you'r help. thanks

Answer

Your EditorFor() method creates an input with id="Request_TravelersCount".

To update its value when the button is clicked, you can use

$(".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)

Comments