Help with asp.net mvc UpdateModel with file upload
I have a form which allows me to upload a picture plus some other data.. in my database I just save a string of the filename... now, when Im editing the same model, should I just let UpdateModel do its job or do I have to specify
modelobject.picture=file.Filename
Something is wrong with my edit form and Im not sure how exactly to debug the UpdateModel method to find the error but Im guessing its got to do with that.
I just realized the exception Im getting is "Row not found or changed" but it doesnt tell me which Row.... The table Im editing has manyy other values but I dont want to change开发者_开发技巧 any of those, so they are not included in the form... and UpdateModel is receving as parameter the fieldsm it should update
Let the (or one of the) parameters in your action method be the model that you are trying to save to the DB. Your posting form will need an enctype="multipart/form-data" attribute. Try this.
//pass in model or use UpdateModel to get non-file info into your model object.
HttpPostedFileBase document = Request.Files[0];
if (document != null && document.ContentLength > 0)
{
document.SaveAs(document.FileName);
model.FileName = document.FileName;
}
SaveModelToDB(model);
精彩评论