GunBladeIV
GunBladeIV

Reputation: 43

Split string on MVC3 + razor view

I would like to get help and guide on how can i manipulate text/string that being called from MS-SQL Database. So here is my SettingController.cs partial code for index viewing:

public ActionResult Index()
    {
        var datacontext = new SGM_SIDDataContext();
        var dataToView = from m in datacontext.SGMs
                         orderby m.Seq
                         select m;
        return View(dataToView.ToList());
    }

And This is my index.cshtml codes:

@model IEnumerable<MVC_Apps.Models.SGM>

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Index</h2>

<p>
    @Html.ActionLink("Create New", "Create")
</p>
<table>
    <tr>
        <th>
            SID
        </th>
        <th>
            Abbrev
        </th>
        <th>
            Val
        </th>
        <th>
            Seq
        </th>
        <th></th>
    </tr>

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.SID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Abbrev)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Val)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Seq)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.GID })
        </td>
    </tr>
}

</table>

So roughly I got a View. Now here is what i would like to do: 1) The data 'item.Val' on Index.cshtml view will be like this:

A,L,C,H

and sort of like that. But the might be line where the data only contain:

H or C or none(Null value)

But if the data contain more than one char like say it does set for K and L, the data in item.Val will look like this:

A,L

Which the data being separate by comma. So now i want to split that item.Val data and do if statement on it. Where I would like to check every data in item.Val if it contain K or L or C or H or all of them or none of them(Sorry if my english is bad). And I would like to view it as , if all the data contain H so in table view, the will be column named Hotel while if the data also have L so another column of Lounge will be display with a check button being checked.

the possible char in item.Val is:

A = Admin
L = Lounge
H = Hotel
C = Customer

Any help and ideas much appreciated. Thank you in advanced.

Update information:

Thanks Im starting to get what is it. What i really want to do is, when the item.Val does contain H(which is for hotel) then the view will have table column with header name Hotel and at the record it will have tick checkbox.

This is sample picture of table view: http://imagebin.org/152941

But then the Hotel, Admin and User information either it is tick or not is in item.Val For example for Smith, the item.Val data look like this : C, For example for Anna , the item.Val data look like this : H,C,

P.S : - the var conf line is a test code. Ignore it as I already delete it from my source. :)

Upvotes: 1

Views: 3397

Answers (1)

Ahmad
Ahmad

Reputation: 24827

Create a ViewModel similar to

public class MyViewModel
{
  // items from your model
  public int Id{get;get;}
  public String Vals{get;set;} // A,L,C,H
  ...
  ...

  public bool IsHotel
  {
    get
    {
       return Vals.Split(',').Contains("H");
    }
  }   

  public bool IsAdmin      
  {
    get
    {
       return Vals.Split(',').Contains("A");
    }
  } 

  public bool IsCust
  {
    get
    {
       return Vals.Split(',').Contains("C");
    }
  } 

}

In the controller action

public ActionResult Index()
{
   var datacontext = new SGM_SIDDataContext();
   var dataToView = from m in datacontext.SGMs
                    orderby m.Seq
                    select new MyViewModel()
                    {
                      // items from your datacontext
                      Id= m.SID, 
                      //etc...

                    };
   var conf = from m in datacontext.SGMs // what's the purpose of this query??
                     select m.Val;

   return View(dataToView.ToList());
}

The View will need to be updated, so include columns for Hotel, Cust, Admin and any others you need. I would suggest that you keep it simple and don't try to create the HTML columns on the fly. If you want to do this, you probably will need to inspect all Vals in every object of your list first to determine what columns are needed. In the view

@foreach (var item in Model)
{
    <tr>
        <td>
            @Html.DisplayFor(modelItem => item.SID)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Abbrev)
        </td>
        <td>
          @(item.IsHotel? "Checked" : "")
        </td>
        <td>
          @(item.IsAdmin? "Checked" : "")
        </td>
        <td>
          @(item.IsCust? "Checked" : "")
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.Seq)
        </td>
        <td>
            @Html.ActionLink("Edit", "Edit", new { id=item.GID })
        </td>
    </tr>
}

Upvotes: 2

Related Questions