lundi 11 janvier 2016

Create from Existing MVC

So i have my model and i want to create a new plant from an existing plant selected from a drop down list with the same properties as the old plant except the scientific name.I created a function that will take the new plant name and the old plant and creates a new plant with old properties.I want to know what should i do in my controller and view in order to create this new plant from existing.

Here is my model

public class ExistingPlant
{

    public string selectedPlant { get; set; }
    public Dictionary<Guid,string> plantList { get; set; }
}

and here is my controller code

public ActionResult CreateFrom()
    {
        ExistingPlant existing = new ExistingPlant();

        Dictionary<Guid, string> plants = new Dictionary<Guid, string>();

        foreach(var item in context.Plants){

            plants.Add(item.PlantId , item.ScientificName);
             }

        existing.plantList = plants;
        existing.selectedPlant = "Value";

        var plant = context.Plants.Where(d => d.ScientificName == existing.selectedPlant);
        string temp = existing.selectedPlant;
        Plant p = (Plant) plant;
        CopyExisting(p,temp);

        return View(existing);
    }

and my view code :

@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset id="field">
    <div>
        <label> Scientific Name:</label>
    </div>

    <div class="editor-field">
        @Html.EditorFor(m => m.selectedPlant)
        @Html.ValidationMessageFor(m =>m.selectedPlant)
    </div>

    <p>
        <input type="submit" value="Create" />
    </p>

</fieldset>
@Html.DropDownListFor(
    m => m.selectedPlant,
    new SelectList(Model.plantList, "Key", "Value")
)

}

Aucun commentaire:

Enregistrer un commentaire