Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using javascript to assign value to model

i'm using a calendar and I need to pass the value from it into my model to be submitted back to the controller but i'm stuck.

View

<div class="cell">
    @Html.LabelFor(model => model.ExpectedDatetimeStamp, new { @class = "control-label col-md-2" }):
    <div data-role="calendar" data-week-start="1" data-multi-select="false" id="c1" class="calendar"></div>
        <div hidden>@Html.EditorFor(model => model.ExpectedDatetimeStamp)</div>
    <div>
        @Html.ValidationMessageFor(model => model.ExpectedDatetimeStamp)
    </div>
</div>

<div>
    <input type="submit" value="Create" class="button primary" />
</div>

JS

function get_dates(){
    var result = $("#c1").calendar('getDates');
    alert(result);
}

how can I assign the value from result to model.ExpectedDatetimeStamp when the user presses the submit button?

like image 811
Tsukasa Avatar asked Apr 01 '26 19:04

Tsukasa


1 Answers

As per comments, each item in your model is given a name that matches its name in the model. So for example, your item ExpectedDatetimeStamp would have an element with that name.

So to set the value, simply use:

$('#ExpectedDatetimeStamp').val(result);

And done! :)

like image 88
Jane S Avatar answered Apr 03 '26 07:04

Jane S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!