vendredi 14 août 2015

mvc input one to many

I'd like to get some help with this :

<table>
<tr>
    <td>
        <select id="specieName" name="specieName">
            foreach(var item in Model)
            {
                <option>@item.potatoSpecie</option>
            }
        </select>
    </td>
    <td><input size=25 type="text" id="potatoName" name="potatoName"/></td>
    <td><input size=25 type="text" id="potatoSize" name="potatoSize"/></td>
</tr>

public class potato
{
    public potato()
    {
        category - new HashSet<category>();
    }
    public string name name { get; set; }
    public virtual ICollection<Category> Category { get; set; }
    public int IdPotato { get; set; }
}

public class category
{
    [Key]
    public int IdPotato { get; set; }
    public int potatoSize { get; set; }
    public virtual potatoSpecie potatoSpecie { get; set; }
}

public class specie
{
    public string specieName { get; set; }
    public int specieId { get; set; }
}
public ActionResult Potato(string submit)
{
    var new hotPotato = new potato();
    {
        hotPotato.category.potatoSize = int.Parse(Request.Form["potatoSize"]);
        hotPotato.name = Request.Form["potatoName"];
        hotpotato.category.specie.specieName - Request.Form["specieName"];
    }
    using(potatoesContext context - new potatoesContext())
    {
        context.potato.add(potato)
        context.SaveChanges();
    }
    return View(potato);
}

From here, there's a few things I'd like to understand, can I retrieve both razor and regular fields with one action ? is there a better way to do it than Request.Form? The biggest problem from here is that I don't exactly know where to go after that, I'm trying to put these potatoes in my database and I'd like to at least be sure if I'm on the right direction, thanks beforehand.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire