How can I fix this error in asp.net mvc -
inside index.cshtml
, have dropdownlist
populated data. on selection of dropdownlist
item, loading partial views using ajax
. these partial views forms, when filled, data inserted database. after insertion, want go index.cshtml
has populated dropdownlist
initially, doesn't work , gives error:
an exception of type 'system.invalidoperationexception' occurred in system.web.mvc.dll not handled in user code additional information: there no viewdata item of type 'ienumerable' has key 'templatescategory'.
here controller ----
public class templatecontroller : controller { pulsecontext db = new pulsecontext(); public actionresult index() { viewbag.templatescategory = new selectlist(db.templates, "id", "templatename"); return view(); } [httpget] public partialviewresult clothing() { return partialview("_clothingtemplate"); } [httppost] public actionresult clothing(templateclothing tc) { db.templateclothings.add(tc); db.savechanges(); return view("index"); }
here view -----
@model pulse.models.templates @{ viewbag.title = "index"; } <div class="container"> <script src="~/scripts/jquery-2.2.3.min.js"></script> <script src="~/scripts/bootstrap.min.js"></script> <h2>select template</h2> @html.dropdownlist("templatescategory") <script type="text/javascript"> $(document).ready(function () { $("#templatescategory").change(function () { var choice = this.value; if (choice === "1") { $.ajax({ url: '/template/footwear', contenttype: 'application/html;charset=utf-8', type: 'get', datatype:'html' }) .success(function(result){ $('#templatecontainer').html(result) }) } else if (choice === "2") { $.ajax({ url: '/template/accessory', contenttype: 'application/html;charset=utf-8', type: 'get', datatype: 'html' }) .success(function (result) { $('#templatecontainer').html(result) }) } else if (choice === "3") { $.ajax({ url: '/template/clothing', contenttype: 'application/html;charset=utf-8', type: 'get', datatype: 'html' }) .success(function (result) { $('#templatecontainer').html(result) }) } }); }); </script> <div id="templatecontainer"></div> </div>
here model --
namespace pulse.models { using system; using system.collections.generic; public partial class templates { public templates() { this.templateaccessoriestbls = new hashset<templateaccessories>(); this.templateclothingtbls = new hashset<templateclothing>(); this.templatefootweartbls = new hashset<templatefootwear>(); } public int id { get; set; } public string templatename { get; set; } public virtual icollection<templateaccessories> templateaccessoriestbls { get; set; } public virtual icollection<templateclothing> templateclothingtbls { get; set; } public virtual icollection<templatefootwear> templatefootweartbls { get; set; } } }
please me how can fix error , also, how redirect index page populated dropdown.
in case done call index
method before rendering view
want
return redirecttoaction("index");
instead of : return view("index");
so clothing should below
[httppost] public actionresult clothing(templateclothing tc) { db.templateclothings.add(tc); db.savechanges(); return redirecttoaction("index"); }
this recall index
method , render view
want
Comments
Post a Comment