我的模型中的列表在HttpPost方法中不可用 [英] A list in my model is not available in the HttpPost method

查看:63
本文介绍了我的模型中的列表在HttpPost方法中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我有一个模型,每个模型都来自一个基类,它存储一个翻译列表。



Hello,

I have a model and each model derives from a base class, that stores a list of translations.

public abstract class BaseObject
{
  public List<LocalizationPart> Translations { get; set; }
}

public class LocalizationPart
{
  public string Culture { get; set; }
  public List<Translation> Translations { get; set; }
}

public class Translation
{
  public string Property { get; set; }
  public string Text { get; set; }
}

public class CivilStatus : BaseObject
{
  public string Name { get; set; }
  public string Description { get; set; }
}





此翻译列表在获取或创建对象期间被填充。



现在我有一个编辑屏幕,从这个翻译列表中,我得到了我需要的翻译。 (例如:在CivilStatus对象上,我可以有一个名称和描述属性,因此每个属性都可以按语言进行翻译。)因此,在创建和编辑页面中,我列出了下面的相应编辑字段。 Html.Label(...)





this Translations list gets filled during the get or create of an object.

Now I have an edit screen and from this translations list, I get the translations that I need. (Example: on the object CivilStatus, I can have a name and description property, so each property can have a translation per language.) So in the create and edit page I list the appropriate edit fields below the @Html.Label(...).

<div class="editor-label">
  @Html.Label(T(property.Name.
</div>
<div class="editor-field">
  @if (Attribute.IsDefined(property, typeof(LocalizableAttribute)))
  {
    foreach (var translation in Model.Translations)
    {
       @Html.Label(translation.Culture, new {@class = "vt-cultureid"})
       var translation = translation.Translations.SingleOrDefault(t => t.Property == property.Name);
       if (contentItemVersionRecord != null)
       {
         @Html.EditorFor(m => contentItemVersionRecord.Translation)<br />
       }
    }
  }
</div>





但是当我按下保存按钮时,翻译当我在HttpPost动作中查看它时,list为null。





But when I press the save button, the translations list is null when I look at it in the HttpPost action.

[HttpPost]
public ActionResult Create(CivilStatus civilstatus) { ... }





我怎样才能做到翻译绑定到模型?



How can I make it that the translations are bound to the model?

推荐答案

为了做到这一点,我不得不用for循环遍历两个列表,而不是foreach循环。如果我使用



To do it right, I had to iterate over the two lists with a for loop, not a foreach loop. If I use

@Html.TextBoxFor(m => m.Translations[i].Translations[j].Translation)





它确实正确传递了它。



为了允许使用接口,我必须创建自己的模型绑定器。



更多信息可以在这里找到文章: ASP.NET MVC模型绑定的特性和缺点 [ ^ ]


这篇关于我的模型中的列表在HttpPost方法中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆