How do I update a model value in JavaScript in a Razor view?

后端 未结 3 1934
野性不改
野性不改 2020-12-25 10:44

I want to update model value in JavaScript as below but it is not working.

function updatePostID(val)
{
    @Model.addcomment.PostID = val;
}
3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-25 11:06

    You could use jQuery and an Ajax call to post the specific update back to your server with Javascript.

    It would look something like this:

    function updatePostID(val, comment)
    {
    
        var args = {};
        args.PostID = val;
        args.Comment = comment;
    
        $.ajax({
         type: "POST",
         url: controllerActionMethodUrlHere,
         contentType: "application/json; charset=utf-8",
         data: args,
         dataType: "json",
         success: function(msg) 
         {
            // Something afterwards here
    
         }
        });
    
    }
    

提交回复
热议问题