Search code examples
c#asp.net-mvcentity-frameworkhtml-helpereditorfor

MVC 5 EF 6 EditorFor Dropdown List Change Text displayed


I have what should be a simple set of data from a table in SQL to a HTML.EditorFor helper. The problem I have is the records in the database are boolean values and I need to display a string of text depending on if the value is a 1 or a 0.

My Editor Template looks like this:

@model nData.DAL.tblTaggingGroupInstance
<td>
    @Html.DisplayFor(modelItem => modelItem.WeekDay)
</td>
<td>
    @Html.DisplayFor(modelItem => modelItem.Segment)
</td>

<td>
    @Html.DisplayFor(modelItem => modelItem.NetSales)
</td>
<td>
    @Html.DisplayFor(modelItem => modelItem.SwellSales)
</td>
<td>
    @Html.DisplayFor(modelItem => modelItem.SpikeSales)
</td>
<td>
    @Html.EditorFor(modelItem => modelItem.Recurring)
</td>
<td>
    @Html.EditorFor(modelItem => modelItem.tblTaggingReasonID)
</td>
<td>
    @Html.EditorFor(modelItem => modelItem.Comment)
</td>

My Model is:

public partial class tblTaggingGroupInstance
{
    public int ID { get; set; }
    public int tblTaggingGroupID { get; set; }
    public int FinYear { get; set; }
    public int FinWeek { get; set; }
    public int WeekDay { get; set; }
    public Nullable<int> tblTaggingReasonID { get; set; }
    public Nullable<decimal> NetSales { get; set; }
    public Nullable<decimal> SwellSales { get; set; }
    public Nullable<decimal> SpikeSales { get; set; }
    public string Comment { get; set; }
    public string Segment { get; set; }
    public Nullable<bool> Recurring { get; set; }
    public Nullable<decimal> BaseSales { get; set; }

    public virtual tblTaggingGroup tblTaggingGroup { get; set; }
}

And the Controller is

public ActionResult Index()
{
    var SelectedID = Convert.ToInt32(Session["_SessionSelectGroupID"]);
    var SelectedYear = Convert.ToInt32(Session["_SessionSelectFinYear"]);
    var SelectedWeek = Convert.ToInt32(Session["_SessionSelectFinWeek"]);
    var tblTaggingGroupInstances = db.tblTaggingGroupInstances.Include(t => t.tblTaggingGroup);

    return View(tblTaggingGroupInstances.Where(t => t.tblTaggingGroupID == SelectedID && t.FinYear == SelectedYear && t.FinWeek == SelectedWeek).ToList());
}

[HttpPost]
public ActionResult Index(List<tblTaggingGroupInstance> model)
{
    foreach (var record in model)
    {
        db.Entry(record).State = EntityState.Modified;
    }
    db.SaveChanges();
    return RedirectToAction("Index");
}

What would be the simplest / easiest way to to change the text that is displayed without affecting the value that is posted back to the database?

Thanks Mark


Solution

  • Inside your model, you can create a corresponding string (getter) type property of your bool property. Which will always give you the corresponding string you defined. i.e,

    public Nullable<bool> Recurring { get; set; }
    public string RecurringText
    { get { 
            if(Recurring.HasValue && Recurring.Value == true)
               return "Yes";
            else
               return "No";
           }
    }
    

    and in your EditorTemplate, you can do as:

    @Html.LabelFor(m => m.RecurringText)
    @Html.HiddenFor(m => m.Recurring)
    

    Please Note if this property is editable, you can use TextboxFor instead of LabelFor. as:

    @Html.TexboxFor(m => m.RecurringText)
    

    and in your JQuery script tag, you need to set the corresponding bool value to the hidden Recurring property on save event. something like:

    //inside click of your save button:
    var inputRecurringValue = $('#ReccuringText').val();
    if(inputRecurringValue.toLower() == 'yes')
    {  
       $('#Recurring').val(true);
    }
    else
       $('#Recurring').val(false);