Pakk
Pakk

Reputation: 1339

mvc Partial views and model issues

model 1 dbclient

clientID,client(name),clientcontact , and an ICollection Countys

model 2 dbcounty

countyID,clientID,county(name),countyOther

Overview:

Creating a Client View that has a (View) Countys actionlink - done + working

Creating a County View that has a (create,edit,delete,details) actionlink - done + partially working

to get to my Countys:

@Html.ActionLink("Countys", "Index", "County", new { id=item.ClientID },null) 

from countys to get to my New:

@Html.ActionLink("Create New", "Create", new { id = Model.ClientID})

from controller to view ( passing clientID not CountyID )

 public ActionResult Create(int id=0)
    {
        dbClient Client = db.Clients.Find(id);
        if (Client == null)
        {
            return HttpNotFound();
        }
        return View(Client);
    }

in the view: Here is the issue , being that i want to create a county and not use an icollection of countys from clients but at the same time would like to show what client we are creating this county for

@model OilNGasWeb.ModelData.dbClient 

@{
ViewBag.Title = "Create";
}

<h2>Create County</h2>

@Html.Partial("_CreateCounty", Model.County) <----** Here ** Model.County is a string and Model.Countys is an icollection, i just want the normal model ???

<div>
@Html.ActionLink("Back to List", "Index", new { id=Model.ClientID})
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

the partial view: Seems to be freaking out because an Icollection or string is being passed -.- but i jsut want the model because im assuming thats what i need for the MVC Framework to create the insert properly?

@model OilNGasWeb.ModelData.dbCounty

@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<fieldset>
    <legend>Countys</legend>
  //data
  //data
  //data
</fieldset>

Im sorry if this doesnt make sence please advise what you need of me still learning mvc

Allow me to revise the question above in something i know better

Think of a vb.net application

(you have clients and there id's in a list) after selecting a client you want to show another list ( this time editable ) with countys information (now you only want to grab countys information, create an insert line for the database with the clients chosen clientID)

that explain it any better, having a hard time creating something like this for MVC

another option would be somehow on create new county, to include a dropdown list of clients ( and their respective IDs) and using the id along with the editable county info?

Upvotes: 1

Views: 428

Answers (1)

Ethan Shafer
Ethan Shafer

Reputation: 68

Since you're tracking which counties belong to each client, I don't see why you would need to pass anything besides the ClientID for creation.

So your create view might look something like

@model OilNGasWeb.ModelData.dbClient 

@{
    ViewBag.Title = "Create";
}

<h2>Create County</h2>

@Html.Partial("_CreateCounty", new OilNGasWeb.ModelData.dbCounty() { clientID = Model.ClientID } ) <!-- pass the client ID to the partial

<div>
    @Html.ActionLink("Back to List", "Index", new { id=Model.ClientID})
</div>

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

For your Edit/Delete/View Details views, you should be able to just pass the CountyID or the object itself from the model.

Upvotes: 2

Related Questions