asp.net - Object reference not set to an instance of an object -
this question has answer here:
- what nullreferenceexception, , how fix it? 32 answers
i new asp.net mvc , cannot find solution how make selectable dropdown list.
my model:
public class parentcategory { public int id { get; set; } [required] [display(name = "parent category name")] public string pcatname { get; set; } [required] [display(name = "urlseo")] public string urlseo { get; set; } [required] [display(name = "description")] public string pcatlogo { get; set; } public icollection<childcategory> childcategories { get; set; } } public class addparentcategoryviewmodel { public int id { get; set; } public string pcatname { get; set; } public string urlseo { get; set; } public string pcatlogo { get; set; } }
my controller
[httpget] public actionresult addnewpcat() { list<int> numlist = new list<int>(); int num = 0; var newid = num; addparentcategoryviewmodel model = new addparentcategoryviewmodel(); model.id = newid; return view(model); } [httppost] [validateantiforgerytoken] [validateinput(false)] public actionresult addnewpcat([bind(include = "id,pcatname,urlseo,pcatlogo")]addparentcategoryviewmodel model, httppostedfilebase file) { var filename = path.getfilename(file.filename); var path = path.combine(server.mappath("~/image/pcatlogo/"), filename); file.saveas(path); model.pcatlogo = file.filename; var pcategory = new parentcategory { id = model.id, pcatname = model.pcatname, urlseo = model.urlseo, pcatlogo = model.pcatlogo }; _pagerepository.addnewparentcategory(pcategory); return redirecttoaction("index", "page"); }
my repository is:
public void addnewparentcategory(parentcategory pcategory) { _context.parentcategories.add(pcategory); save(); }
and view:
@model bandymas.models.pageviewmodels.addparentcategoryviewmodel @using bandymas.controllers; @{ viewbag.title = "add new pcat"; layout = "~/views/shared/_layout.cshtml"; } @section scripts { <script src="~/scripts/view.js"></script> <script src="~/ckeditor/ckeditor.js"></script> } @using (html.beginform("addnewpcat", "page", null, formmethod.post, new { role = "form" })) { @html.antiforgerytoken() <div class="editpostcontainer"> <table> @*<tr> <td>id :</td> <td colspan="2" class="editpageid">@html.textboxfor(m => m.id, new { @class = "editidinp", @readonly = "readonly" })</td> </tr>*@ <tr> <td>pcatname :</td> <td colspan="2" class="editpageusername">@html.textboxfor(m => m.pcatname, new { @class = "editpcatnameinp" })</td> </tr> <tr> <td>urlseo :</td> <td colspan="2">@html.textboxfor(m => m.urlseo, new { @class = "editurlseo" })</td> </tr> <tr> <td>pcatlogo :</td> @*<td colspan="2">@html.textboxfor(m => m.pcatlogo, new { @class = "editpcatlogo" })</td>*@ <td> <input id="imagepath" title="upload category image" type="file" name="file" /> </td> </tr> <tr> <td></td> <td colspan="3" class="editpagebody"><input class="comtextbtn" type="submit" value="➥" /></td> </tr> </table> </div> }
something wrong file upload. problem is:
an exception of type 'system.nullreferenceexception' occurred in bandymas.dll not handled in user code additional information: object reference not set instance of object.
it first time face problem , cannot find solution anywhere... thank in advance.
you need add enctype="multipart/form-data"
form post file like
html.beginform( action, controller, formmethod.post, new { enctype="multipart/form-data"})
Comments
Post a Comment