Wednesday, December 30, 2015

How to upload a file in an ASP.Net MVC






step_1
file id uploadFile



on ProfilePicture.cshtml



step_2 [HttpPost]
public ActionResult ProfilePicture()
{
HttpPostedFileBase photo = Request.Files["uploadFile"];
string uploadedPath = "Content\\images\\profileimages\\";
string sFileDir = System.Web.HttpContext.Current.Server.MapPath("~") + uploadedPath;
if (photo.ContentLength > 0)
{
string sFileName = System.IO.Path.GetFileName(photo.FileName);
photo.SaveAs(sFileDir + sFileName);
}
}




step_3 in Request.Files["uploadFile"];



and enjoy

Tuesday, December 22, 2015

Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.

I have found error on execute this code DbContext.GL_tbl_Addresses.Attach(Address); DbContext.Entry(Address).State = Address.userid == 0 ? EntityState.Added : EntityState.Modified; DbContext.SaveChanges();


Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries.






the best solution replace Address.userid with GL_tbl_Addresses table primary id eg ID DbContext.Entry(Address).State = Address.Id == 0 ? EntityState.Added : EntityState.Modified; and run your code.

@html.dropdownlistfor set selected value


step (1)


public class ContactInformation { public string State { get; set; } public IEnumerable StateList { get; set; } }


step (2) @Html.DropDownListFor(model => model.State, Model.StateList, "--Select--", new { @class = "form-control" })


step (3) IEnumerable listState = from a in DbContext.GL_tbl_States.OrderBy(u => u.Name) select new SelectListItem { Text = a.Name, Value = a.ShortName };


ContactInformation.StateList = listState.Select(l => new SelectListItem { Selected = (l.Value == ContactInformation.State), Text = l.Text, Value = l.Value });





and run your project.