MVC 4,模型和模型呈现不同

时间:2013-07-22 06:06:31

标签: asp.net-mvc-4 model

我不确定为什么我会这样做,如果有人能指出我正确的来源,我很高兴,因为我不确定需要谷歌。

查看模型

public class GeneralDay
{
    public byte tintWeekDay { get; set; }
    public string nvarDayName { get; set; }
    public bool Assigned { get; set; }
}

控制器

    [ChildActionOnly]
    public PartialViewResult TargetDay(MODELS.ViewModels.Promotion promotion) {

        var daylist = gd.GeneralDayList();
        foreach (var day in daylist)
        { 
            promotion.targetDay.Add(new MODELS.ViewModels.GeneralDay
            {
                Assigned = promotion.targetDay.FirstOrDefault(x => x.tintWeekDay == day.tintWeekDay) != null,
                nvarDayName = day.nvarDayName,
                tintWeekDay = day.tintWeekDay
            });
        }

        ModelState.Clear();
        return PartialView(promotion);
    }

查看

@model GeneralDay
@using XXX.ViewModels;

<fieldset>


 //First Part 
@(Model.tintWeekDay)
@(Model.nvarDayName)
@(Model.Assigned)

//Second Part
@Html.LabelFor(model => model.tintWeekDay)    
@Html.LabelFor(model => model.nvarDayName)    
@Html.LabelFor(model => model.Assigned) 

</fieldset>

以上显示这些结果,为什么会有不同的结果?

第一部分

  

1星期天假

第二部分

  

tintWeekDay

     

nvarDayName

     

分配

2 个答案:

答案 0 :(得分:1)

以下打印值..

//First Part 
@(Model.tintWeekDay)
@(Model.nvarDayName)
@(Model.Assigned)

以下是创建标签

//Second Part
@Html.LabelFor(model => model.tintWeekDay)    
@Html.LabelFor(model => model.nvarDayName)    
@Html.LabelFor(model => model.Assigned) 

由于您尚未使用DisplayAttributeLabelFor只是打印变量名称。

也许你想用:

//Second Part
@Html.DisplayFor(model => model.tintWeekDay)    
@Html.DisplayFor(model => model.nvarDayName)    
@Html.DisplayFor(model => model.Assigned) 

答案 1 :(得分:0)

因为在第一部分中,您渲染了模型属性的值。

@(Model.tintWeekDay)将1呈现给输出。

但@ Html.LabelFor(model =&gt; model.tintWeekDay)为此字段呈现(不存在的)标签,而不是值)

使用DisplayAttribute更改模型中的字段标签。

示例:

public class GeneralDay
{
    [Display(Description = "Day of week")]
    public byte tintWeekDay { get; set; }
    public string nvarDayName { get; set; }
    public bool Assigned { get; set; }
}