1

I am trying to accomplish what I thought would be simple. I just cannot wrap my mind around it.

I have a list of organizations, it is just the OrganizationID and Name. What I wanted to do was have an "add" link, much like an edit, that is next to each organization (just like the delete and edit buttons).

It would grab the OrganizationID and pass it to one of my Create Views in other controllers and automatically fill in just the field for OrganizationID.

The add link would simply point to a View that has a DropDownList. The user would then select one of the options, for example Address, to add an address to the Organization that was selected from the list of Organizations. After the user selects Address, they hit the submit button. Then it goes to the OrganizationAddress Create View and fills in the OrganizationID field automatically.

I really hope I explained it well enough.

This is the Add ActionResult in my OrganizationController

public ActionResult Add(int id)
{
    using (var db = new VAGTCEntities())
    {
        ViewBag.OrgPages = ddp.PageList();
        return View(db.Organizations.Find(id));
    }
}

Grabs the Organization.

Here is the form in my Add view:

    <fieldset>
        <legend>Organization</legend>

        @Html.HiddenFor(model => model.OrganizationID)

        <div class="editor-label">
            @Html.Label("Add:")
        </div>
        <div>
            @Html.DropDownList("OrgPages")
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

Here is the Add POST ActionResult in my OrganizationController

[HttpPost]
public ActionResult Add(int id, Organization organization, int OrgPages)
{

    try{
        ViewBag.OrgID = id;
        if (OrgPages == 2)
            return RedirectToAction("Create", "OrgAddress");
        else if (OrgPages == 3)
            return RedirectToAction("Create", "OrgIE");
        else if (OrgPages == 4)
            return RedirectToAction("Create", "OrgMembership");
        else if (OrgPages == 5)
            return RedirectToAction("Create", "OrgBusinessType");
        else if (OrgPages == 6)
            return RedirectToAction("Create", "OrgIndustryType");
        else if (OrgPages == 7)
            return RedirectToAction("Create", "OrgPhone");
        else if (OrgPages == 8)
            return RedirectToAction("Create", "OrgEmail");
        else if (OrgPages == 9)
            return RedirectToAction("Create", "OrgWebsite");
        else if (OrgPages == 9)
            return RedirectToAction("Create", "OrgNote");
        else                   
            return RedirectToAction("Index");
    }
    catch (Exception)
    {
        ModelState.AddModelError("Error", "This item exists in the database already.");
        ViewBag.OrgPages = ddp.PageList();
    }
    return View(organization);
}

From here I cannot figure it out. Anything I try to pass turns out null. I am still new to MVC - slowly, slowly learning my way!

Thank you very much!

4

1 に答える 1

1

Do you have a custom route to account for the following parameters you are passing?

public ActionResult Add(int id, Organization organization, int OrgPages)

In your view, you can either specify what action the form will be posting to and include the parameters OR just add those properties (i.e. Organization, OrgPages) to the view model and then let MVC do all the binding for you.

Read this for details

于 2012-11-20T02:47:09.473 に答える