简体   繁体   中英

Grab post back collection and show it on list over webpage

I have an MVC webpage with a DropDownList full of items. Every item is an object from my Database that represent a file on disk.

My object class:

namespace CapturesMVC.Models

public class Capture : IEquatable<Capture>
{
    public int id { get; set; }
    [Display(Name = "File Name")]
    public string fileName { get; set; }

    [Display(Name = "Browser")]
    public string browser { get; set; }

    [Display(Name = "Mobile")]
    public string mobile { get; set; }

    [Display(Name = "Protocol")]
    public string protocol_site { get; set; }

    public string family { get; set; }

    public sealed override bool Equals(object other)
    {
        return Equals(other as Capture);
    }

    public bool Equals(Capture other)
    {
        if (other == null)
        {
            return false;
        }
        else
        {
            return this.protocol_site == other.protocol_site;
        }
    }

    public override int GetHashCode()
    {
        return protocol_site.GetHashCode();
    }
}

CaptureDBContext class:

namespace CapturesMVC.Models

public class CaptureDBContext : DbContext
{
    public DbSet<Capture> Captures { get; set; }
}

This is my controller:

[HttpPost]
public ActionResult Index(string File)
{
    var list = db.Captures.Where(x => x.protocol== File).ToArray();
    ViewBag.Files = list;
    return View();
}

Index.cshtml:

@using (Html.BeginForm())
{ 
    <div>   
        @Html.DropDownList("File", new SelectList(ViewBag.Files, "protocol_site", "protocol_site"), "Select webmail site", new { style = "vertical-align:middle;" })
        <button type="submit">Select</button>
    </div>
}
</body>

After choosing an item from my DropDownList and hitting the button, the Index action is executed and returns list of objects that match one of my object properties and this list I want to show over my webpage inside a list, but the current situation is that this list is inserted into my DropDownList .

The problem is that you put the objects in the same ViewBag property that your Dropdownlist gets its values from.

You could make a List and put that in your ViewBag :

List<Capture> list = db.Captures.Where(x => x.protocol== File).ToList();
ViewBag.data = list;

And enumerate over these and display some html in your view (within an unordered list for example). But you have to cast it back to a list first:

@using Namespace.Capture
...
<ul>
foreach (var item in (ViewBag.data as List<Capture>))
{
    <li>@item.Property</li>
}
</ul>

ViewBag is a C# 4 dynamic type. You need to cast the entities from it to use them in a type-safe way.

But I would recommend using a view model with the list as a property and sending that to the view from your controller action.

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