1

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

5
  • Are you referring to the Nullable<bool> Recurring property? What text do you want to display? Commented Feb 2, 2016 at 11:03
  • Yes that's the one, I need it to say Recurring or Non-Recurring depending on if the value is 1 or 0. Commented Feb 2, 2016 at 11:05
  • Its nullable, so what do you want to display when its null? One option would be to use a view model with an additional string property containing the text and include a hidden input for the property (but if you want to edit it and update the display text, then you will need javascript) Commented Feb 2, 2016 at 11:09
  • Some other options using HtmlHelper extension methods and DisplayTemplates shown in the answers to this question Commented Feb 2, 2016 at 11:14
  • please update the title of your question, to specify what you actually need. I see dropdown in your title, but now in your code Commented Feb 2, 2016 at 11:34

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

You could send your booleans as hidden fields and just display a text for their value.

// Razor View / Editor Template    
@{ var recurringDisplay = Model.Recurring ? "recurring" : "not recurring"; }

@recurringDisplay
@Html.HiddenFor(m => m.Recurring)

As Stephen Muecke noted, you will need some Javascript to update the display value if the value of Recurring can be changed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.