【问题标题】:MVC3 Postback doesn't have modified dataMVC3 Postback 没有修改过的数据
【发布时间】:2023-04-02 04:23:01
【问题描述】:

所以我有以下代码:

@model Project.Models.ViewModels.SomeViewModel
@using (Html.BeginForm("SomeAction", "SomeController", new { id = Model.Id}))
        {
            for(int i = 0; i < Model.SomeCollection.Count(); i++)
            {
                @Html.HiddenFor(x => Model.SomeCollection.ElementAt(i).Id)
                <div class="grid_6">
                    @Html.TextAreaFor(x => Model.SomeCollection.ElementAt(i).Text, new { @style = "height:150px", @class = "grid_6 input" })
                </div>
            }
            <div class="grid_6 alpha omega">
                <input type="submit" value="Next" class="grid_6 alpha omega button drop_4 gravity_5" />
            </div>
        }

在控制器端,我有以下内容:

[HttpPost]
        public ActionResult SomeAction(int id, SomeViewModel model)
        {

            return PartialView("_SomeOtherView", new SomeOtherViewModel(id));
        }

我的视图模型是这样设置的:

public class SomeViewModel
{
        public SomeViewModel()
        {

        }
        public IEnumerable<ItemViewModel> SomeCollection { get; set; }

}

public class ItemViewModel{

      public ItemViewModel(){}

      public int Id {get;set;}

      public string Text{get;set;}

}

如果执行 SomeAction,SomeCollection 始终为空。为了显示用户更新的值,我必须做什么。文本属性和 ID 字段。

【问题讨论】:

    标签: asp.net-mvc-3 postback


    【解决方案1】:

    使用EditorTemplate

    在您的 Views/YourcontrollerName 下创建一个 EditorTemplate 文件夹,并创建一个名称为 ItemViewModel.cshtml

    的视图

    并且在那个文件中有这段代码

    @model  Project.Models.ViewModels.ItemViewModel
    <p>
     @Html.EditorFor(x => x.Text) 
     @Html.HiddenFor(x=>x.Id)
    </p>
    

    现在从您的主视图中,这样称呼它

    @model  Project.Models.ViewModels.SomeViewModel
    @using (Html.BeginForm("SomeAction", "Home", new { id = Model.Id}))
    {
        @Html.EditorFor(s=>s.SomeCollection)
        <div class="grid_6 alpha omega">
            <input type="submit" value="Next" class="grid_6 alpha omega button drop_4 gravity_5" />
        </div>
    }
    

    现在在您的HTTPPOST 方法中将填充值。

    我不确定你想对这些值做什么(返回部分视图?)所以不要对此进行任何 cmets。

    【讨论】:

      【解决方案2】:

      我不确定你是否已经发布了所有代码。

      您的操作方法不执行任何操作,因为它使用新模型对象返回部分视图(出于某种原因来自 post 调用,而不是 ajax 请求)。

      您将模型有效地传递回操作,然后丢弃它,并返回一个新的模型对象。这就是您的收藏总是空的原因,它永远不会设置在任何地方。

      【讨论】:

      • 我认为他只是为了简洁而遗漏了一些代码。这不是他的问题。
      【解决方案3】:

      好吧,一方面,为什么将模型和id(模型的属性)都发送回控制器?是不是显得有点多余?此外,您正在视图中使用 javascript for 循环。使用@foreach 会容易得多。

      无论如何,您的问题是,当您告诉操作接受模型时,它会在 post 中查找键与模型的每个属性的名称匹配的值。所以,假设我们有以下模型:

      public class Employee
      {
         public string Name;
         public int ID;
         public string Position;
      }
      

      如果我像这样传回去:

      @using(Html.BeginForm("SomeAction", "SomeController"))
      {
           <input type="text" name = "name" [...] />    //in your case HtmlHelper is doing this for you, but same thing
           <input type="number" name = "id" [...] />
           <input type="submit" name = "position" [...] />
      }
      

      要将此模型传回控制器,我必须这样做:

      接受模型

      //MVC matches attribute names to form values
      public ActionResult SomethingPosted(Employee emp)
      {
          //
      }
      

      接受值的集合

      //MVC matches parameter names to form values
      public ActionResult SomethingPosted(string name, int id, string postion)
      {
          //
      }
      

      或者这个:

      接受 FormCollection

      //same thing as first one, but without a strongly-typed model
      public ActionResult SomethingPosted(FormCollection empValues)
      {
          //
      }
      

      所以,这里有一个更好的代码版本。

      您的新观点

      @model Project.Models.ViewModels.SomeViewModel
      @{
          using (Html.BeginForm("SomeAction", "SomeController", new { id = Model.Id}))
              {
                  foreach(var item in Model)
                  {
                      @Html.HiddenFor(item.Id)
                      <div class="grid_6">
                          @Html.TextAreaFor(item.Text, new { @style = "height:150px", @class = "grid_6 input" })
                      </div>
                  }
                  <div class="grid_6 alpha omega">
                      <input type="submit" value="Next" class="grid_6 alpha omega button drop_4 gravity_5" />
                  </div>
              }
      }
      

      您的新操作

      [HttpPost]
      public ActionResult SomeAction(int Id, string Text)
      {
          //do stuff with id and text
          return PartialView("_SomeOtherView", new SomeOtherViewModel(id));
      }
      

      [HttpPost]
      public ActionResult SomeAction(IEnumerable<ItemViewModel> SomeCollection)  //can't use someviewmodel, because it doesn't (directly) *have* members called "Id" and "Text"
      {
          //do stuff with id and text
          return PartialView("_SomeOtherView", new SomeOtherViewModel(id));
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-20
        相关资源
        最近更新 更多