1

I'm working with a SelectList, and populating it with the data from a table. I'm trying to bind it to the ID of another object.

EDIT Updated the Schema to Reflect something I neglected. I have updated it to show the exact names of each item. I think the problem comes in the fact that each Unit has a sheet, and each sheet has a product. (The sheet will hold more information for the product, but a Unit will have a great deal of other information, so I wanted to separate them in order to keep it clear what was what.)

I think something is happening and the "Sheet" isn't being initialized as an object, and when the binding occurs, it doesn't have an object to bind to, since it is one-level deep. Tell me if this makes sense, or if I am just completely off base.

**Unit**
UnitID (PK)

**ProductSheet**
UnitId (FK)(PK)
ItemId (FK)

**Items**
ItemId (PK)
ItemTitle

It just ...isn't working though. I have this code.

DatabaseDataContext db = new DatabaseDataContext();
Unit unit = new Unit();
ViewData["Items"] = new SelectList( db.Items, "Id", "ItemTitle", unit.ProductSheet.ItemId);

But in the postback, the selectList is always null and empty! This is the View code. I'm really getting lost here, I've followed a lot of examples and it still comes up with bunk.

<%= Html.DropDownList("Items") %>
7
  • It will obviously house more items in the future, 10 is just the test data. It keeps posting back null on create. Commented Sep 21, 2009 at 20:52
  • Stacey: check the edit made to the question in post stackoverflow.com/questions/1434734/…. The MVC engine has a specific order it uses to obtain values. Unfortunately, this can sometimes gets in the way. Commented Sep 21, 2009 at 20:53
  • As strange as this sounds, please change ViewData["Items"] to ViewData["MyItems"] or something similar. I want to rule out name clashes. Commented Sep 21, 2009 at 20:57
  • The UnitItemId is in the Model. And to David Andres, If I use Request.Form["Items"], I get the proper Id returned as a string. But I know for certain this is not how it is intended to be used, I should be able to explicitly bind right to the model. – Stacey 0 secs ago Commented Sep 21, 2009 at 21:02
  • @Stacey, What happens when you change the name of the Items key to ViewData["MyItems"]? Commented Sep 21, 2009 at 21:04

3 Answers 3

3

Your view code should read:

<% var selectItems = ViewData["Items"] as SelectList; %>
<%= Html.DropDownList("ProductSheet.ItemId", selectItems) %>
Sign up to request clarification or add additional context in comments.

16 Comments

Sorry, that doesn't even compile.
DropDownList will only accept a string as it's first parameter.
With your edit, it compiles and runs but the result is the same. It still comes out null on the model after posting.
Stacey: just so I'm clear, what do you mean by "comes out null on the model after posting?"
I run it, type in the values, pick the item from the dropdown. I hit "submit". Break into the debugger. The model.Sheet.ItemId is simply null. But, if I do Request.Form["Items"], I'll get the ID of the item that was selected in the dropdown.
|
0

An entire sample of the project is available at this url

These are the action methods.

public ActionResult Create()
{
        DatabaseDataContext database = new DatabaseDataContext();
        Unit u = new Unit();
        u.Sheet = new Sheet();
        ViewData["ProductListing"] = new SelectList(database.Products, "ProductId", "ProductName", u.Sheet.ProductId);
    return View();
} 

//
// POST: /Home/Create

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Unit u)
{
    try
        {
            DatabaseDataContext database = new DatabaseDataContext();
            database.Units.InsertOnSubmit(u);
            database.SubmitChanges();
        // TODO: Add insert logic here

        return RedirectToAction("Index");
    }
    catch
    {
        return View();
    }
}

Here is the Create View.

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MVC.Models.Contexts.Unit>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

    <h2>Create</h2>

    <%= Html.ValidationSummary("Create was unsuccessful. Please correct the errors and try again.") %>

    <% using (Html.BeginForm()) {%>

        <fieldset>
            <legend>Fields</legend>
            <%= Html.DropDownList("ProductListing") %>
            <p>
                <input type="submit" value="Create" />
            </p>
        </fieldset>

    <% } %>

    <div>
        <%=Html.ActionLink("Back to List", "Index") %>
    </div>

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="ScriptContent" runat="server">
</asp:Content>

Comments

0

I've marked the answer that seems to have made some progress now, I think I'm starting to understand it better.

When I passed the 'path' of the object to assign as a string in the name field of DropDownList, it seems to work. But this doesn't really make a lot of sense to me. Can you explain what is going on any better?

3 Comments

By path, do you mean ProductSheet.ItemId? I think it helps to understand what happens when you post a form item whose name is ProductSheet.ItemId. Let's say you have an action method that takes a productsheet parameter. MVC will automatically equate the ProductSheet.ItemId form field with the productSheet action method parameter, and which this form data will try to fill the parameter's ItemId property (if it exists). This will work even if ProductSheet is a property of the paraemter itself, as in myParam.ProductSheet.ItemId. It's confusing, I know.
I think the biggest problem is that field name and object name are sharing the same parameter, and it's a simple string. It isn't very clear to the programmer that that parameter needs to match the property you want to associate with in your model, and that you can reference the object itself (i.e. "Sheet.ProductId" instead of just being limited to "Sheet" or "ProductId").
Is there any way to do this and control the id field of the html output?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.