C# 使用修改的模型渲染视图

C# 使用修改的模型渲染视图,c#,asp.net,asp.net-mvc,asp.net-mvc-4,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc,Asp.net Mvc 4,Asp.net Mvc 5,我是MVC新手,我的工作方式很简单,但我有个问题, 出于某种原因,我想更改控制器操作中的绑定模型,然后 渲染视图,但不会发生这种情况 例如: public class Product{ int id {get; set;} string description {get; set;} } 以及控制器方法: [POST] public ActionResult Edite(Product p){ p.description = "HELLOOOOOO!!!" re

我是MVC新手,我的工作方式很简单,但我有个问题, 出于某种原因,我想更改控制器操作中的绑定模型,然后 渲染视图,但不会发生这种情况

例如:

public class Product{
    int id {get; set;}
    string description {get; set;}
}
以及控制器方法:

[POST]
public ActionResult Edite(Product p){
    p.description = "HELLOOOOOO!!!"
    return View(P);
}
观点:

@model WebSite.Models.Product

@{
    ViewBag.Title = "Modificar (Product)";
}

<h2>Modificar (Product)</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <h4></h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.Id)

        <div class="form-group">
            @Html.LabelFor(model => model.description, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.description, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.description, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Guardar" class="btn btn-default" />
            </div>
        </div>
    </div>
}

<div>
    @Html.ActionLink("Volver a la Lista", "Index")
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}
@model.WebSite.Models.Product
@{
ViewBag.Title=“修改(产品)”;
}
修改(产品)
@使用(Html.BeginForm())
{
@Html.AntiForgeryToken()

@Html.ValidationSummary(true,“,new{@class=“text danger”}) @Html.HiddenFor(model=>model.Id) @LabelFor(model=>model.description,htmlAttributes:new{@class=“controllabel col-md-2”}) @EditorFor(model=>model.description,new{htmlAttributes=new{@class=“form control”}) @Html.ValidationMessageFor(model=>model.description,“,new{@class=“text danger”}) } @ActionLink(“Volver a la Lista”,“Index”) @节脚本{ @Scripts.Render(“~/bundles/jqueryval”) }
但是在渲染视图中,产品的描述不是“Hellooo!!!!”


为什么MVC使用用户输入的值而不是我想要的新模型渲染视图?

可能不是解决问题的最佳方法,但这篇文章解决了我的问题:

您必须向我们显示
Edite
视图。我已经编辑了您的标题。请参阅“”,其中的共识是“不,他们不应该”。您将模型发布到
Edite()
。如果您想要返回一个新模型,那么遵循PRG模式并重定向到GET方法并构建一个新模型。当您发布并返回视图(而不是模型属性)时,MVC绑定到
ModelState
值。谢谢John,对不起,我是Stackoverflow的新手。发布的代码是一个示例,不是我的真实案例,但是,我将添加视图示例。@StephenMuecke感谢您的回答,是的,我在考虑这个选项,但是,在我的真实案例中,我想呈现一个部分视图,并且我想在不重新加载页面的情况下使用ajax,在这种情况下是否可以使用PRG?