File upload
public class EventViewModel : UserAttendEvent
{
public int EventId { get; set; }
public int UserId { get; set; }
[Required]
public string EventTitle { get; set; }
[Required]
public string Location { get; set; }
[Required]
[DataType(DataType.DateTime)]
public DateTime StartDate { get; set; }
[Required]
[DataType(DataType.DateTime)]
public DateTime EndDate { get; set; }
public string Details { get; set; }
public string Image { get; set; }
[DataType(DataType.Upload)]
public string File { get; set; }
public string IsActive { get; set; }
public string OrganizerName { get; set; }
public string OrganizerUserName { get; set; }
}
public class UserAttendEvent
{
public int AttendId { get; set; }
public string ButtonValue { get; set; }
}
@model Net.Models.ViewModel.MauritiusHikers.EventViewModel
@{
ViewBag.Title = "PostEvent";
Layout = "~/Views/Shared/UserMasterPage.cshtml";
}
@using (Html.BeginForm("PostEvent", "User", FormMethod.Post, new{ enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="row">
<div class="col-lg-12">
<h1 class="page-header">
Post An Event
</h1>
</div>
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.EventTitle, "Title", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.EditorFor(model => model.EventTitle, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EventTitle, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.EditorFor(model => model.Location, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Location, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.StartDate, "Start Date", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.EndDate, "End Date" , htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Details, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.TextAreaFor(model => model.Details, new { @class = "form-control", @rows = "10" })
@Html.ValidationMessageFor(model => model.Details, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.File, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-6">
@Html.TextBoxFor(model => model.File, new { type = "file" })
@Html.ValidationMessageFor(model => model.File, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-5">
<input type="submit" value="Post" class="btn btn-primary" />
</div>
</div>
</div>
</div>
}
[HttpPost]
public ActionResult PostEvent(EventViewModel item)
{
var user = Session["user"] as UserViewModel;
if (user == null)
return RedirectToAction("HomePage", "Home");
if (ModelState.IsValid)
{
item.UserId = user.UserId;
foreach (string file in Request.Files)
{
HttpPostedFileBase uploadedFile = Request.Files[file] as HttpPostedFileBase;
if (uploadedFile.ContentLength == 0)
continue;
string savedFileName = Path.Combine(eventImagePath, Path.GetFileName(uploadedFile.FileName));
uploadedFile.SaveAs(savedFileName);
item.Image = Path.GetFileName(uploadedFile.FileName);
}
string endPoint = _endPoint.BuildUri(domain, routePrefix, routePostEvent, port);
var httpWebRequest = (HttpWebRequest)WebRequest.Create(endPoint);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = JsonConvert.SerializeObject(item);
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
try
{
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
EventViewModel result = JsonConvert.DeserializeObject<EventViewModel>(streamReader.ReadToEnd());
if (result != null)
return RedirectToAction("Home", "User");
else
return RedirectToAction("EventProcessFail", "HttpCodeStatus");
}
}
catch (WebException ex)
{
HttpWebResponse httpResponse = (HttpWebResponse)ex.Response;
if (httpResponse.StatusCode == HttpStatusCode.BadRequest)
{
return RedirectToAction("EventProcessFail", "HttpCodeStatus");
}
if (httpResponse.StatusCode == HttpStatusCode.InternalServerError)
{
return RedirectToAction("EventProcessFail", "HttpCodeStatus");
}
}
}
return View();
}