如何在Razor视图中更新JavaScript中的模型值?

时间:2022-06-03 11:14:56

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

我想在JavaScript中更新模型值,如下所示,但它不起作用。

function updatePostID(val)
{
    @Model.addcomment.PostID = val;
}

in Razor view as shown below

在刀片视图中,如下所示

foreach(var post in Model.Post)
{
    <br/>
    <b>Posted by :</b> @post.Username <br/>
    <span>@post.Content</span> <br/>
    if(Model.loginuser == Model.username)
    {
        @Html.TextAreaFor(model => model.addcomment.Content)
        <button type="submit" onclick="updatePostID('@post.PostID');">Add Comment </button>
    }
}

Can anyone tell me how to assign model value in JavaScript?

有人能告诉我如何在JavaScript中分配模型值吗?

3 个解决方案

#1


38  

This should work

这应该工作

function updatePostID(val)
{
    document.getElementById('PostID').value = val;

    //and probably call document.forms[0].submit();
}

Then have a hidden field or other control for the PostID

然后为PostID设置一个隐藏字段或其他控件

@Html.Hidden("PostID", Model.addcomment.PostID)
//OR
@Html.HiddenFor(model => model.addcomment.PostID)

#2


11  

The model (@Model) only exists while the page is being constructed. Once the page is rendered in the browser, all that exists is HTML, JavaScript and CSS.

模型(@Model)仅在构建页面时存在。在浏览器中呈现页面之后,剩下的就是HTML、JavaScript和CSS。

What you will want to do is put the PostID in a hidden field. As the PostID value is fixed, there actually is no need for JavaScript. A simple @HtmlHiddenFor will suffice.

你想要做的是把PostID放到一个隐藏的字段中。由于PostID值是固定的,实际上不需要JavaScript。一个简单的@HtmlHiddenFor就足够了。

However, you will want to change your foreach loop to a for loop. The final solution will look something like this:

但是,您需要将foreach循环更改为for循环。最终的解是这样的:

for (int i = 0 ; i < Model.Post; i++)
{
    <br/>
    <b>Posted by :</b> @Model.Post[i].Username <br/>
    <span>@Model.Post[i].Content</span> <br/>
    if(Model.loginuser == Model.username)
    {
        @Html.HiddenFor(model => model.Post[i].PostID)
        @Html.TextAreaFor(model => model.addcomment.Content)
        <button type="submit">Add Comment</button>
    }
}

#3


2  

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

您可以使用jQuery和Ajax调用将特定的更新发送回服务器。

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

     }
    });

}

#1


38  

This should work

这应该工作

function updatePostID(val)
{
    document.getElementById('PostID').value = val;

    //and probably call document.forms[0].submit();
}

Then have a hidden field or other control for the PostID

然后为PostID设置一个隐藏字段或其他控件

@Html.Hidden("PostID", Model.addcomment.PostID)
//OR
@Html.HiddenFor(model => model.addcomment.PostID)

#2


11  

The model (@Model) only exists while the page is being constructed. Once the page is rendered in the browser, all that exists is HTML, JavaScript and CSS.

模型(@Model)仅在构建页面时存在。在浏览器中呈现页面之后,剩下的就是HTML、JavaScript和CSS。

What you will want to do is put the PostID in a hidden field. As the PostID value is fixed, there actually is no need for JavaScript. A simple @HtmlHiddenFor will suffice.

你想要做的是把PostID放到一个隐藏的字段中。由于PostID值是固定的,实际上不需要JavaScript。一个简单的@HtmlHiddenFor就足够了。

However, you will want to change your foreach loop to a for loop. The final solution will look something like this:

但是,您需要将foreach循环更改为for循环。最终的解是这样的:

for (int i = 0 ; i < Model.Post; i++)
{
    <br/>
    <b>Posted by :</b> @Model.Post[i].Username <br/>
    <span>@Model.Post[i].Content</span> <br/>
    if(Model.loginuser == Model.username)
    {
        @Html.HiddenFor(model => model.Post[i].PostID)
        @Html.TextAreaFor(model => model.addcomment.Content)
        <button type="submit">Add Comment</button>
    }
}

#3


2  

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

您可以使用jQuery和Ajax调用将特定的更新发送回服务器。

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

     }
    });

}