How to manage files in edit mode ASP.NET MVC Entity Framework? -
i working on asp.net mvc web application uses entity framework.
here model class:
public partial class complaint { public int complaintid { get; set; } public string reference { get; set; } public string clientname { get; set; } public string accountno { get; set; } public nullable<system.datetime> datecomplaint { get; set; } public nullable<decimal> value { get; set; } public nullable<int> typeid { get; set; } public string comment { get; set; } public nullable<int> statusid { get; set; } public nullable<system.guid> insertedby { get; set; } public nullable<system.datetime> createddatetime { get; set; } public nullable<system.datetime> modifieddatetime { get; set; } public virtual icollection<fileattach> fileattaches { get; set; } } controller method:
[authorize] [authorizeroles("admin", "oficial")] public actionresult requestapproval(int? id) { viewbag.status = _context.status.tolist(); //viewbag.user = _context.profiles.where(x => x.user.userroles.firstordefault().roleid == 2 || x.user.userroles.firstordefault().roleid == 3).tolist(); if (id == 0) { return new httpstatuscoderesult(httpstatuscode.badrequest); } complaint c = _context.complaints.include(s => s.fileattaches).singleordefault(x => x.complaintid == id); if (c == null) { return httpnotfound(); } return view(c); } [httppost] [validateantiforgerytoken] public actionresult requestapproval(int? id, httppostedfilebase upload) { var c = _context.complaints.find(id); if (tryupdatemodel(c, "", new string[] { "reference", "clientname", "accountno", "datecomplaint", "value", "typeid", "comment", "statusid", "isapproved", "approvedby", "insertedby", "updatedby", "createddatetime", "modifieddatetime" })) { (int = 0; < request.files.count; i++) { upload = request.files[i]; if (upload != null && upload.contentlength > 0) { stream str = upload.inputstream; binaryreader br = new binaryreader(str); byte[] filedet = br.readbytes((int32)str.length); var filename = path.getfilename(upload.filename); fileattach filedetail = new fileattach() { name = filename, contenttype = upload.contenttype, data = filedet, attachid = new int(), path = path.combine(server.mappath("~/uploads/"), filename), complaintid = id }; var path = path.combine(server.mappath("~/uploads/"), filedetail.name); upload.saveas(path); _context.entry(filedetail).state = entitystate.added; } } _context.entry(c).state = entitystate.modified; _context.savechanges(); return redirecttoaction("list"); } return view(c); } my view:
<div data-parsley-validate class="form-horizontal form-label-left"> @using (html.beginform("requestapproval", "complaint", null, formmethod.post, new { enctype = "multipart/form-data" })) { @html.antiforgerytoken() @html.validationsummary(true, "", new { @class = "text-danger" }) @html.hiddenfor(model => model.complaintid, new { @class = "id" }) <div class="form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name"> @html.labelfor(model => model.reference) <span class="required">*</span> </label> <div class="col-md-6 col-sm-6 col-xs-12"> @html.dropdownlistfor(model => model.statusid, new selectlist(viewbag.status, "statusid", "denomination"), "- please select type -", new { @class = "form-control" }) </div> </div> <div class="form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name"> @html.labelfor(model => model.comment) <span class="required">*</span> </label> <div class="col-md-6 col-sm-6 col-xs-12"> @html.textareafor(model => model.comment, new { @class = "form-control col-md-7 col-xs-12", style = "width:100% auto; height:150px;" }) </div> </div> <div class="form-group"> <label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name"> files </label> <div class="col-md-6 col-sm-6 col-xs-12"> <input type="file" name="file" id="fileupload" multiple="multiple" /> <ul class="attachment"> @foreach (var item in model.fileattaches) { <li> <a class="title" href="/complaint/download/?p=@(item.name)&d=@item.name">@item.name</a> </li> } </ul> </div> </div> <div class="ln_solid"></div> <div class="form-group"> <div class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3"> <button type="submit" class="btn btn-primary btn-sm">back list</button> <button type="submit" class="btn btn-success btn-sm">save <i class="fa fa-save"></i></button> </div> </div> } </div> when try edit error:
server error in '/' application
exception details: system.argumentnullexception: value cannot null.
parameter name: model
wiki
Comments
Post a Comment