ASP.MVC 3 Razor 在 Html.PartialView 扩展中添加模型前缀 [英] ASP.MVC 3 Razor Add Model Prefix in the Html.PartialView extension

查看:17
本文介绍了ASP.MVC 3 Razor 在 Html.PartialView 扩展中添加模型前缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有和他差不多的问题ASP.NET MVC部分视图:输入名称前缀

我正在为此数据实体创建 CRUD 操作:

I am tring to create CRUD operations for this data entities:

public class UserViewModel 
{  
   protected virtual Id {get; set;} 
   public virtual string Login { get; set; }
   public virtual string Password { get; set; }
   public virtual ZipCodeViewModel ZipCode { get; set; }
}

邮政编码实体:

public class ZipCodeViewModel
{        
   public virtual string City { get; set; }

   public virtual string State { get; set; }           

   public virtual string Zip { get; set; }
}

我也有使用 UserViewModel.ZipCode 的部分视图 ZipCode:

I also have partial view ZipCode which used UserViewModel.ZipCode:

@model ZipCodeViewModel    

    @Html.TextBoxFor(x => x.Zip, new { id = "ZipCode", name = "ZipCode.Zip", maxlength = "5" })     

我将在另一个页面(例如用户)中使用 ZipCodePartialView.

I am going to use ZipCodePartialView in another Page (for example user).

@model UserViewModel
.....
@{Html.RenderPartial("Zipcode", Model.ZipCode);}

当 MVC 保存 User ZipCode 字段时,Zip 为空.

When MVC save User ZipCode field Zip is empty.

那么问题是如何向父视图添加模型前缀?

So question is how I can add model prefix to the patrial View?

推荐答案

试试这个扩展:

public static void RenderPartialWithPrefix(this HtmlHelper helper, string partialViewName, object model, string prefix)
        {
            helper.RenderPartial(partialViewName,
                                 model,
                                 new ViewDataDictionary { TemplateInfo = new System.Web.Mvc.TemplateInfo { HtmlFieldPrefix = prefix } });
        }

这篇关于ASP.MVC 3 Razor 在 Html.PartialView 扩展中添加模型前缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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