using javascript to assign value to model

时间:2015-05-12 22:43:34

标签: c# jquery entity-framework asp.net-mvc-4

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

.AsQueryable

JS

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

how can I assign the value from result to function get_dates(){ var result = $("#c1").calendar('getDates'); alert(result); } when the user presses the submit button?

2 个答案:

答案 0 :(得分:1)

As per comments, each item in your model is given a name that matches its name in the model. So for example, your item lblSVal1.Txt = top3.ElementAt(0).NetScore.ToString(); lblSVal2.Txt = top3.ElementAt(1).NetScore.ToString(); lblSVal3.Txt = top3.ElementAt(2).NetScore.ToString(); would have an element with that name.

So to set the value, simply use:

ExpectedDatetimeStamp

And done! :)

答案 1 :(得分:0)

The HTML helper you use will assign the input the id of POST. 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 ExpectedDatetimeStamp. Further, in order to properly assign a value to the input as opposed to the moment object, you may want to use getDate

.format()