发布忽略模型更改Razor ASP.NET

时间:2015-05-25 03:24:06

标签: c# asp.net asp.net-mvc asp.net-mvc-4 razor

我有一个控制器,它返回一个带有模型的部分视图。

包含部分视图的视图有一个按钮,当单击按钮时,将调用控制器的一个功能,并返回相同的部分视图,并更新模型。加载新模型没有任何问题,但页面没有重新加载,视图与onclik之前的视图相同。

部分视图的代码

<div class="well">
    @if (publication.Coments != null) {
         foreach (var comments in publication.Coments) {
             <div class="row">
                  <div class="col-md-12">
                       <a href="../../Client/ProfilePerNick?nick=@comments.Nick">@comments.Nick</a>
                       <span class="pull-right">@comments.DateComment.ToShortDateString()</span>
                       <p>@comments.Message</p>
                   </div>
              </div>
         }
    }
</div>

控制器的方法使用下一个代码返回局部视图:

   ViewData["publication"] = publication;
   return PartialView("details_comment");

我在视图中调用局部视图:

 @Html.Partial("../Home/ListPublication")

我调试了页面,模型重新加载好了,但部分视图没有重新加载。

4 个答案:

答案 0 :(得分:1)

尝试如下。

Form2

答案 1 :(得分:1)

我在评论中提到我遇到了同样的问题,但今天晚些时候我在MSDN上发现,如果您返回相同的模型并在{{1}之后查看类型,这是预期的行为}。对于我的场景,我必须在更改返回视图模型上的任何值之前使用POST。为了更好地解释一下,我会尽量描述我的情况,尽可能地记住尝试语境化:

查看模型

ModelState.Clear()

模板

// ~/Models/SomeFeatureModels.cs
public class SomeViewModel {
    public int Id {get;set;}
    public string SomeField{get;set;}
    public string SomeOtherField{get;set;}
    public DateTime CreatedOn{get;set;}
}
public class SomeOtherViewModel {
    public int Id {get;set;}
    public string SomeField{get;set;}
    public string SomeOtherField{get;set;}
    public DateTime CreatedOn{get;set;}
}
public class IndexViewModel {
    public string FeatureTitle{get;set;}
}

<强>控制器

<!-- ~/Views/Some/SomeInfo.cshtml -->
@model.App.Models.SomeInfoViewModel
@using(Html.BeginForm("AddSomeInfo", "Some", FormMethod.Post, new { @id="frmAddSomeInfo" }) {
  <div id="some-info">
    @Html.DisplayFor(m=>m.SomeField)
    @Html.EditorFor(m=>m.SomeField)
    @Html.ValidatorFor...
    <input type="submit">Go</input>
  </div>
}

<!-- ~/Views/Some/SomeInfo.cshtml -->
@model.App.Models.SomeOtherInfoViewModel 
@using(Html.BeginForm("AddSomeOtherInfo", "Some", FormMethod.Post, new { @id="frmAddSomeOtherInfo" }) {
  <div id="some-other-info">
    @Html.DisplayFor(m=>m.SomeField)
    @Html.EditorFor(m=>m.SomeField)
    @Html.ValidatorFor...
    <input type="submit">Go</input>
  </div>
}

<!-- ~/Views/Some/Index.cshtml -->
@model App.Models.IndexViewModel
@{
   layout: "someLayout.cshtml"
}
<h2>Model.FeatureTitle</h2>

@{ RenderAction("SomeInfo") }
@{ RenderAction("SomeOtherInfo") }

@section scripts {
//bundle must include:
// jquery, jquery.unobtrusive.ajax, jquery.validate, jquery.validate.unobtrusive
<script>
    $(function() {
        $('#frmAddSomeInfo').submit(function(e) {

            e.preventDefault(); 

            var form = $(this);
            if (form.valid()) {
                $.ajax({
                    url: form.action,
                    type: form.method,
                    data: form.serialize()
                }).done(function(payload) {
                    $('#some-info').html(payload);
                }).fail(function(jqXHR, error, errorThrown) {
                    // handle
                });
            }
        });

        $('#frmAddSomeOtherInfo').submit(function(e) {

            e.preventDefault(); 

            var form = $(this);
            if (form.valid()) {
                $.ajax({
                    url: form.action,
                    type: form.method,
                    data: form.serialize()
                }).done(function(payload) {
                    $('#some-other-info').html(payload);
                }).fail(function(jqXHR, error, errorThrown) {
                    // handle
                });
            }
        });
    });
</script>
}

这是我必须使用// ~/Controllers/SomeController.cs public class SomeController: Controller { // This would be the landing view of a given controller public ActionResult Index() { // for this view model I have basically typed the things that // are a concern of Index, like page title and other things // but nothing related to the view models that I will be // updating or inserting var viewModel = somePrivateMethodToBuildMyViewModel(); return View(viewModel); } public PartialViewResult SomeInfo() { // this is technically a fresh instance with normalized // or localized default data that I will be sending // when the index requests this partial view var someViewModel = somePrivateMethodToBuildSomeViewModel(); return PartialView(someViewModel); } [HttpPost] public PartialViewResult AddSomeInfo(SomeViewModel viewModel) { // this is where I am checking if my view model is alright // and then the "issue" will occur if (!ModelState.IsValid) { // ... handle } else { // I was doing "normal stuff" like // translating view model to an entity (let's call it model) // committing changes with some ORM and get and id and timestamp back // and naturally assign these values to the view model viewModel.Id = model.id; viewModel.createdOn = model.created_on; } // then return the same view and same model **types** of the request return PartialView("SomeInfo", viewModel); } } 的部分。我已将ModelState.Clear()操作更改为:

POST

对不起,这有点太多了,但我只是想表明我是如何让它在我的场景中运作的。

答案 2 :(得分:0)

您需要使用局部视图传递模型的对象以查看值,因为您的模型将与帮助程序绑定以创建视图

return PartialView("details_comment",publication);

答案 3 :(得分:0)

更新

而不是相对网址:

Try: @Html.Partial(@"~/Views/Home/ListPublication.cshtml") or use 
@{Html.RenderPartial("ListPublication");} 

我注意到的另一件事是你使用旧的ViewData。我并不是说你根本不应该使用它,但更好的方法是使用强类型的ViewModel。视图模型是一个类,它具有视图呈现所需的属性和逻辑。所以你可以上课:

public class PublicationVM{public Publication publication {get; set;} }

并且如果视图需要,可以具有其他必要的属性。 SO有很多关于使用View Model的信息。请检查What is ViewModel in MVC?