set a value to model using jQuery

前端 未结 2 1916
南方客
南方客 2021-02-02 17:46

How can I set a value to my model using jQuery?

I have an input field (which its id=\"comment\") and I want the text in it to be inserted into @Model.Comment

2条回答
  •  孤城傲影
    2021-02-02 18:47

    Far be it from me to disagree with Darin (he's answered half my questions on here!) but will put this up in case the OP or anyone else finds it useful.

    By giving an Html attribute to a model value:

    @Html.HiddenFor(x => x.Object.Id, new { id = "Id" } )
    

    You can then set the value with Jquery like so

    $("#Id").val(5); // or whatever value
    

提交回复
热议问题