lundi 22 juin 2015

CRUD operation in a single view in ASP.Net MVC

Im trying to create a simple asp crud application but i dont want to create servral views.This is how i do it:

i have a view named index and a controller named HomeController

public class HomeController : Controller
{
    private readonly InventoryContext _db = new InventoryContext();
    public ActionResult Index()
    {  
        return View();
    }


    [HttpPost]
    public ActionResult Create()
    {
        //ItemType is a class
        var itemType = new ItemType { IsBarCode = true, ItemTypeName = "fsdfadfadf" ;
        return View("Index",itemType);
    }

    [HttpPost]
    public ActionResult Save()
    {
        _db.SaveChanges();
        return RedirectToAction("Index");
    }
}

}

and i view i bind my buttons to controller actions:

<input type="submit" value="Create" onclick="location.href='@Url.Action("Create", "Home")'"  class="button btn btn-default" />

<input type="submit" value="Save" onclick="location.href='@Url.Action("Save", "Home")'" class="button btn btn-success" />

The problem is the Create action look for /Home/Create view after user submit data too server using httppost,but i want to redirect it to same view that called it(index view here).

Aucun commentaire:

Enregistrer un commentaire