asp.net mvc - How to add partialview to insert data -
i have 2 tables category , product.
category:- categoryid,categoryname product:- productid, productname, price, categoryid(fk)
i want add data single view. create category partial view , render create page.
controller is:-
public class productcontroller : controller { private storedbentities db = new storedbentities(); // // get: /product/ public actionresult index() { var products = db.products.include(p => p.category); return view(products.tolist()); } // // get: /product/details/5 public actionresult details(int id = 0) { product product = db.products.find(id); if (product == null) { return httpnotfound(); } return view(product); } // // get: /product/create public actionresult create() { viewbag.categoryid = new selectlist(db.categories, "categoryid", "categoryname"); return view(); } public actionresult createcategory() { return view(); } [httppost] public actionresult createcategory(category category) { if(modelstate.isvalid) { db.categories.add(category); db.savechanges(); return redirecttoaction("create"); } return view(category); } // // post: /product/create [httppost] [validateantiforgerytoken] public actionresult create(product product) { if (modelstate.isvalid) { db.products.add(product); db.savechanges(); return redirecttoaction("index"); } viewbag.categoryid = new selectlist(db.categories, "categoryid", "categoryname", product.categoryid); return view(product); }
}
and create view is:-
@model mvcapplication9.models.category @html.partial("_category",model); @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true) <fieldset> <legend>category</legend> <div class="editor-label"> @html.labelfor(model => model.categoryname) </div> <div class="editor-field"> @html.editorfor(model => model.categoryname) @html.validationmessagefor(model => model.categoryname) </div> <p> <input type="submit" value="create" /> </p> </fieldset> } <div> @html.actionlink("back list", "index") </div>
and _categoy view is:-
@model mvcapplication9.models.category <script src="~/scripts/jquery-1.8.2.min.js"></script> <script src="~/scripts/jquery.validate.min.js"></script> <script src="~/scripts/jquery.validate.unobtrusive.min.js"></script> @using (html.beginform()) { @html.antiforgerytoken() @html.validationsummary(true) <fieldset> <legend>category</legend> <div class="editor-label"> @html.labelfor(model => model.categoryname) </div> <div class="editor-field"> @html.editorfor(model => model.categoryname) @html.validationmessagefor(model => model.categoryname) </div> <p> <input type="submit" value="create" /> </p> </fieldset> } <div> @html.actionlink("back list", "index") </div>
when run show error "validation failed 1 or more entities. see 'entityvalidationerrors' property more details.". please me solve problem. in advance.
Comments
Post a Comment