I am working on an ASP.NET MVC web application that uses Entity Framework.
Here is the 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" }))
{
for (int i = 0; i < 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 a 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 To List</button>
<button type="submit" class="btn btn-success btn-sm">Save <i class="fa fa-save"></i></button>
</div>
</div>
}
</div>
When I try to edit I get this error:
Server Error in '/' Application
Exception Details: System.ArgumentNullException: Value cannot be null.
Parameter name: model