开发者

How to dynamically display a list of both name and value of a String/boolean pair on MVC in ASP.NET MVC

I need to dynamically display a list of both name and value of string/boolean pair on MVC view (*.cshtml) based on user selection. Specifically, both name and value of a string and boolean pair are different in each list. There are more one list that user can select. For example:

FruitName: Apple (string:string)

IsRipen: true (string:boolean)

BookName: C#

IsSold: false

One list type is defined as one report type. A list can be retrieved from report programmatically.

Possible Solution 1 Since the data type of name and value in the list are fixed (string, boolean), one idea is to build a collection as a MVC model, and pass that m开发者_C百科odel to MVC razor view. The question is that how to display the name on the view,

@Html.LabelFor(model => model.Names[0])  //how to display it as 'Fruit Name'

Possible Solution 2 In ASP.NET web form, there is user control whihch can be loaded dynamically. There is partial view in ASP.NET MVC. Can the partial view do what I want? Or is there better solution.

I am new to MVC, any ideal or example would be very much appreicated.


If I understand you correctly, what you want to do is create a Partial View and call it using an action in your controller.

First, do something like this in your controller

    // partial
    public ActionResult report(string reportName)
    {
        return View(reportModel.Name);
    }

Then, make sure there is a partial view that shares the name of your report.

You can then call the partial view like this:

@{Html.RenderAction("report", "Home", new{ reportName="report" });}

The line above will render the partial view Report.cshtml into the parent view or master layout.

edit

Ok. so every report has a list of name value pairs right?

Assuming that, we can write an action that passes that list to your view.

    public ActionResult DisplayPairs()
    {
        NameValueCollection pairs = new System.Collections.Specialized.NameValueCollection();
        pairs.Add("Name", "Value");
        pairs.Add("Name2", "Value2");
        pairs.Add("Name3", "Value3");
        pairs.Add("Name4", "Value4");

        return View(pairs);
    }

Then we have the DisplayPairs View:

@model System.Collections.Specialized.NameValueCollection

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

<h2>DisplayPairs</h2>
<table>
@foreach(string key in Model.AllKeys){
    <tr><th>@key</th><td>@Model[key]</td></tr>
}
</table>

Which displays:

Name Value 
Name2 Value2
Name3 Value3 
Name4 Value4 

I hope this helps


Why don't you just create a wrapper class that contains all the data you need?

public class ReportViewModel
{
    IEnumerable<KeyValuePair<object, object>> Items { get; set; }

    public ReportViewModel()
    { Items = new List<KeyValuePair<object, object>>() }
}

You can then create your model like so:

var model = new ReportViewModel();
model.Items.Add("BookName", "C#");
model.Items.Add("IsSold", false);
return View(model);

In your view, you just iterate over the KeyValuePairs, and print the key and value:

<ul>
@foreach(var kvp in Model.Items)
{
    <li>@kvp.Key: @kvp.Value</li>
}
</ul>

(Excuse me if my razor syntax is buggy - I've not worked very much with it as of yet...)

Also, you might have to add calls to ToSting() if you have odd types of objects in your list. I think the framework does that for you if it needs to, but I'm not sure...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜