简体   繁体   中英

Using asp.net mvc3, how do I display data in a different format to how it's stored in the database?

I'm fairly new to .net, mvc and this forum. So bear with me and thanks in advance.

I have an old database which contains a time field in the form of 'hh:mm' as a string. I cannot change the database as it is used by other applications but I need to read and write that field back to it

Ideally I'd want to split up the hours and minutes and display them as two separately drop down lists. In using mvc3 and entity framework. I though the best idea in this case would be to use a viewmodel, as what I want to return is different from what's directly in my database table, but I'm having a hard time figuring out exactly how to split this element in two, render it, and then stick it together to be updated on the database.

Can anyone recommend any good tips for how to get started on this?

String.Split(':');

will give you an array of 3 strings ["hh", ":", "mm"]

You could extend the view model idea by having somthing like

public class ViewModel
{
public string SelectedHours;
public string SelectedMinutes;
public SelectList AllHours;
public SelectList AllMinutes;
}

you would need to have a list of all the hours and all the minutes and create a new Select List http://msdn.microsoft.com/en-us/library/system.web.mvc.selectlist.aspx

then on your view you could use the @Html.DropDownListFor method.

@Html.DropDownListFor(model => model.SelectedHours, Model.AllHours)

then when you get your results back just create the string need by concatenating the SelectedHours and SelectedMinutes together.

It's probably the easiest way to go about doing that.

Welcome to MVC, you will LOVE it once you get used to it :)

To do this task you will need to do a few things:

  1. Tie the html element in the view to the viewmodel

Example if using Razor (this is in your view):

@Html.LabelFor(m => m.Time)

... if you want dropdowns you'll have to use @Html.DropDownListFor() but then you'll need to create a SelectList for it... see here

2 . Create a ViewModel class and include a property to hold the string value time

3 . In the controller, after the database data returns, call a helper method (or you can do the logic in the action itself if minimal) to split the string and save the results into the viewmodel.

TimeModel vm = new TimeModel();
vm.Time = String.Split(:); // Here is where u could call your helper method or add whatever logic you want to use to make the pretty string value.

Then you return the View and the viewmodel and there you go.

Return View(vm);

Since this is a column in a database, I'd recommend abstracting the logic of this odd time field into it's own class.

class MyTimeField 
{
    public int Hours { get; set; }
    public int Minutes { get; set; }

    public MyTimeField(String timeString)
    {
        var stringParts = timeString.Split(":");
        if(stringParts.length != 2) throw ArgumentException();
        Hours = Int32.Parse(stringParts[0]);
        Minutes = Int32.Parse(stringParts[1]);
    }

    public override String ToString()
    {
        return Hours + ":" + Minutes;
    }
}

Using this class, you can simply create instances of the class as soon as you retrieve the data from the database, and use those in your view, rather than mucking up your code with string parsing and manipulating implementation details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM