3

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?

2
  • 1
    $('#ExpectedDatetimeStamp').val(result); Commented May 12, 2015 at 22:47
  • HA ok so the ID's of each item is the var name? Please post as answer so I can except it. Commented May 12, 2015 at 22:51

2 Answers 2

3

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! :)

Sign up to request clarification or add additional context in comments.

Comments

0

The HTML helper you use will assign the input the id of ExpectedDatetimeStamp. Simply target it in your get dates function, and attach that function in the callback of a submit event on the closest form to the .cell element. If you are using MomentJS, then the call to calendar should be getDate. Further, in order to properly assign a value to the input as opposed to the moment object, you may want to use .format()

function get_dates(){
 var result = $("#c1").calendar('getDate').format();
 $('#ExpectedDatetimeStamp').val(result)
}

$('.cell').closest('form').submit(function(){
 //..any other values that need to be populated
 get_dates();
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.