Monday, 26 August 2013

MVC - using Viewmodel in the view

MVC - using Viewmodel in the view

im trying to use a viewmodel the pass data to my view.
in my view i have a form to add a new item to my database. The form i want
to create have a dropdownlist which receive a list of data from my table
where the user can choice one and the other are textboxes to insert.
my problem is now that im trying to use a viewmodel. but can´t put it to
work.
anybody can give me a hand with this pls?
here is my code:
View:
@model VistaAlegreHotSpotMaker.Models.Test
@using(Html.BeginForm("Create","AddNew", FormMethod.Post)) {
@Html.DropDownListFor("CompanyName",Model.le.CompanyName)
@Html.TextBoxFor(o => o.ti.Title)
@Html.TextBoxFor(o => o.ti.Edition)
@Html.TextBoxFor(o => o.ti.Number)
@Html.TextBoxFor(o => o.ti.Url)
@Html.TextBoxFor(o => o.ti.Images)
<input type="submit" name="submit" value="Adicionar">
}
Controller:
//GET
public ActionResult Create()
{ Test tt = new Test();
return View(tt);
}
//POST
[HttpPost]
public ActionResult Create(TableIndex model)
{
if (ModelState.IsValid)
{
db.ResTableIndex.Add(model);
db.SaveChanges();
return RedirectToAction("Index","AdminPanel");
}
return View(model);
}
ViewModel:
public class Testar
{
public TableIndex ti { get; set; }
public MyCompanies le { get; set; }
}

No comments:

Post a Comment