开发者

MVC2 scaffolding - can't see any data

I'm having a problem seeing any data in my MVC2 application.

I'm using your typical scaffolding setup, but when I view the index or details page I see no data at all.

I'm sure I'm missing something simple here as I get no errors when compiling or running.

Here is my model(Users.cs):

public class Users
{
    public string UserName { get; set; }

    [Required(ErrorMessage = "The Customer ID is required.")]
    public string CustomerID { get; set; }
}

Here are the 2 controller(UsersController.cs) actions that matter:

public class UsersController : Controller
{

    static List<Users> users = new List<Users>();

    //
    // GET: /Users/

    public ActionResult Index()
    {
        return View(users);
    }

    //
    // GET: /Users/Details/5

    public ActionResult Details(Users u)
    {
        return View(u);
    }

}

And here are my views.

Users/Index.aspx

<table>
<% foreach (var item in Model) { %>

    <tr>
        <td>
            <%= Html.ActionLink("Edit", "Edit", new { id=item.UserName }) %> |
            <%= Html.ActionLink("Details", "Details", item )%> |
            <%= Html.Actio开发者_运维百科nLink("Delete", "Delete", new { id = item.UserName })%>
        </td>
        <td>
            <%= Html.Encode(item.UserName) %>
        </td>
        <td>
            <%= Html.Encode(item.CustomerID) %>
        </td>
    </tr>

<% } %>

</table>

<p>
    <%= Html.ActionLink("Create New", "Create") %>
</p>

Users/Details.aspx

<fieldset>
    <legend>Fields</legend>

    <div class="display-label">UserName</div>
    <div class="display-field"><%= Html.Encode(Model.UserName) %></div>

    <div class="display-label">CustomerID</div>
    <div class="display-field"><%= Html.Encode(Model.CustomerID) %></div>

</fieldset>
<p>
    <%= Html.ActionLink("Edit", "Edit", new {  id=Model.UserName }) %> |
    <%= Html.ActionLink("Back to List", "Index") %>
</p>

All the pages load just fine with no errors, but no data is shown. If I run the debugger the users list has 0 entries.

I am using SQL Server. My connection string is in my web.config in the typical fashion:

My database has user data in it already. It was generated with the default asp.net Web Site Administration Tool, which I am using for authentication. This works perfectly.

So what am I doing wrong? Any help is appreciated and if I need to give more info please let me know.


Your UserController (as explained in comment on OP) instantiates your list of users with no data.

If you want to seed data, consider changing the declaration as follows:

public class UsersController : Controller 
{ 

    // static List<Users> users = GetSeededData(); - 
    // don't store a list locally, it isn't necessary if you have a database
    // consider holding the context instead perhaps?
    protected MyContext context = new MyContext();

    // 
    // GET: /Users/ 

    public ActionResult Index() 
    { 
        return View(context.users.ToList()); 
    } 

    // 
    // GET: /Users/Details/5 

    public ActionResult Details(int userId) 
    { 
        return View(context.Users.SingleOrDefault(x => x.UserId.Equals(userId)); 
    } 

} 

A quick suggestion regarding your "persistance" (ignore this if you like, just a friendly tip)...

Consider introducting a persistance layer. That is, if you want to persist data, consider storing it in a database, xml file, etc. Take a look here. It's a great resource on MVC with lots of samples and tutorials, and half way down under the "Models" section it introduces you to the Entity Framework which can be used to store, retrieve, and update your data on a database quite nicely.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜