【问题标题】:MVC ViewModel Custom Property not populating if null exists如果 null 存在,则不填充 MVC ViewModel 自定义属性
【发布时间】:2013-05-06 09:10:25
【问题描述】:

我有一个非常简单的视图模型,基于客户域模型。

我正在向它添加一个属性:CustomerNameAddress,它由域模型的 CustomerName、Contact 和 Town 组成。

但是,如果 CustomerName、Contact 或 Town 中的任何一个为 null - 那么 CustomerNameAddress 也为 null。

是否在下面的查询中检查 null 并将其更改为“”,以便我的视图模型正常工作?

我试过了:

CustomerNameAddress = (p.CustomerName || "") + ", " + (p.Contact || "") + ", " + (p.Town || "")

... 但 VS 建议操作员 '||'不能应用于'string'和'string'类型的操作数

我的控制器代码如下。

谢谢,

标记

    public JsonResult Lookup(string id)
    {
        string userName = null;

    if (HttpContext.User.Identity.IsAuthenticated)
    {
        userName = HttpContext.User.Identity.Name;
    }

    var customers = from p in db.Customers
                    where p.UserName.Equals(userName)
                    select new CustomerLookupViewModel
                    {
                        CustomerId = p.CustomerId,
                        Email = p.Email,
                        CustomerNameAddress = p.CustomerName + ", " + p.Contact + ", " + p.Town
                    };

        return Json(customers, JsonRequestBehavior.AllowGet);
    }

更新

我将代码修改为:

但现在 p.Contact 上出现错误(上面加下划线)建议:无法将类型 'string' 隐式转换为 'bool'

但是,我的视图模型显然将联系人作为字符串:

 public class CustomerLookupViewModel
{
    public string CustomerNameAddress { get; set; }
    public int CustomerId { get; set; }
    [Required]
    [Display(Name = "Customer Name")]
    public string CustomerName { get; set; }
    [Display(Name = "Customer Contact")]
    public string Contact { get; set; }

第二次更新 - 正在运行

我更新了要括在括号中的条件,它现在可以工作了:

CustomerNameAddress = ((p.CustomerName == null) ? "" : (p.CustomerName + ", ")) +
                        ((p.Contact == null) ? "" : (p.Contact + ", ")) +
                        ((p.Town == null) ? "" : (p.Town))

【问题讨论】:

    标签: asp.net asp.net-mvc linq viewmodel


    【解决方案1】:

    可以使用条件运算符? :

     CustomerNameAddress = (p.CustomerName == null || p.Contact == null || p.Town == null ) ? "" : p.CustomerName + ", " + p.Contact + ", " + p.Town
    

    查询会是。

    var customers = from p in db.Customers
                    where p.UserName.Equals(userName)
                    select new CustomerLookupViewModel
                    {
                        CustomerId = p.CustomerId,
                        Email = p.Email,
                        CustomerNameAddress =  (p.CustomerName == null || p.Contact == null ||  p.Town == null ) ? "" : p.CustomerName + ", " + p.Contact + ", " + p.Town
    
                    };
    

    【讨论】:

    • 嗨 Adil - 为此,我得到了错误:错误:运算符'||'不能应用于“bool”和“string”类型的操作数
    • 有一个错字,现在检查一下。
    • 再次感谢 - 使用您和 von v 的示例,我更新了我的问题,以显示控制器和我的视图模型的屏幕截图 - 这给出了指出的错误。我不明白为什么它不起作用。
    【解决方案2】:

    在下面的查询中是否有检查 null 的方法,以及 将其更改为“”,以便我的视图模型正常工作?

    这样的事情应该可以解决问题

    CustomerNameAddress = 
        (string.IsNullOrWhiteSpace(p.CustomerName) ? "" : (p.CustomerName + ", ")) + 
        (string.IsNullOrWhiteSpace(p.Contact) ? "" : (p.Contact + ", ")) + 
        (string.IsNullOrWhiteSpace(p.Town) ? "" : (p.Town + ", "))
    

    您可能希望为此创建一个过程,以便重复使用该简单逻辑。

    【讨论】:

    • 嗨 - 为此我收到错误:错误 1 ​​无法将类型 'string' 隐式转换为 'bool' on: (p.Contact + ", ") + string.IsNullOrWhiteSpace(p.Town)
    • 啊,您需要将每个 if-else 简写括在括号中,抱歉我错过了。
    猜你喜欢
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-27
    • 1970-01-01
    • 2011-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多