Reputation: 409
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")
)
}
Upvotes: 3
Views: 72
Reputation:
You need a property in your view model for the new name, and when you submit the form, get the selected plant, clone it, update the scientific name based on the view model and save it.
Change you view model to
public class CopyPlantVM
{
[Display(Name = "Existing Plant")]
[Required(ErrorMessage = "Please select an existing plant")]
public Guid ExistingPlant { get; set; }
public SelectList ExistingPlantList { get; set; }
[Display(Name = "New scientific name")]
[Required(ErrorMessage = "Please enter new scientific name")]
public string Name { get; set; }
}
and the view to
@model CopyPlantVM
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.ExistingPlant)
@Html.DropDownListFor(m => m.ExistingPlant, Model.ExistingPlantList, "Please select")
@Html.ValidationMessageFor(m => m.ExistingPlant)
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)
@Html.ValidationMessageFor(m => m.Name)
<input type="submit" value="Create" />
}
and the controller methods to
public ActionResult CreateFrom()
{
CopyPlantVM model = new CopyPlantVM();
ConfigureViewModel(model);
return View(model);
}
[HttpPost]
public ActionResult CreateFrom(CopyPlantVM model)
{
if (!ModelState.IsValid)
{
ConfigureViewModel(model);
return View(model);
}
// Get the selected plant
Plant existingPlant = db.Plants.Where(p => p.PlantID = model.ExistingPlant).FirstOrDefault();
// Create a new plant based on existing plant but with new name
Plant newPlant = new Plant
{
ScientificName = model.Name,
// set other properties based on existing plant
};
db.Plants.Add(newPlant);
db.SaveChanges();
return RedirectToAction(...);
}
private void ConfigureViewModel(CopyPlantVM model)
{
model.ExistingPlantList = new SelectList(db.Plants, "PlantId", "ScientificName");
}
Upvotes: 2