tsohtan
tsohtan

Reputation: 850

MVC 4, Model and model render differently

I'm not sure why i get this behavior, happy if anyone could pointing me to right source, as i'm not sure what need to google.

View Model

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

Controller

    [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);
    }

View

@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>

The above display these result , why different result?

First Part

1 Sunday False

Second Part

tintWeekDay

nvarDayName

Assigned

Upvotes: 0

Views: 205

Answers (2)

that guy over there
that guy over there

Reputation: 76

Because in the first part you render the value of the model properties.

@(Model.tintWeekDay) renders 1 to the output.

But @Html.LabelFor(model => model.tintWeekDay) renders the (nonexisting) label for this field, NOT the value)

Use the DisplayAttribute to change the field labels in the model.

Example:

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

Upvotes: 0

Erik Philips
Erik Philips

Reputation: 54618

Well the following prints values..

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

and the following is creating labels

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

Since you haven't used the DisplayAttribute, the LabelFor is just printing the variable name.

Maybe you wanted to use:

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

Upvotes: 1

Related Questions