嗨,我在我的mvc 4应用程序中有这个。一旦你将一个节日添加到系统中,我想通过传递一个id来为该节日添加一个活动。谁能帮我?这是我的控制器以及我的视图,视图模型和模型。我收到一个错误:
参数字典包含'MyFestival中方法'System.Web.Mvc.ActionResult Create2(MyFestival.Models.EventsVM,Int32)'的非可空类型'System.Int32'的参数'festID'的空条目。 Controllers.EventsController”。可选参数必须是引用类型,可空类型,或者声明为可选参数。
[HttpGet]
public ActionResult Create2(int festID)
{
EventsVM events = new EventsVM { festivalID = festID };
events.eType = db.EType.ToDictionary(p => p.ID, q => q.EType);
events.eType.Add(-1, "----- Add New Event Type -----");
events.eventsDate = DateTime.Now;
events.startTime = DateTime.Now;
events.endTime = DateTime.Now;
return View(events);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create2(EventsVM model, int festID)
{
if (ModelState.IsValid != true)
{
if (model.selectedEType != -1)
{
//db.save stuff from create.
Events Newevent = new Events();
Newevent.EndTime = model.endTime;
Newevent.StartTime = model.startTime;
Newevent.EventsDate = model.eventsDate = DateTime.Now;
Newevent.EventsName = model.EventsName;
Newevent.EType = db.EType.Where(p => p.ID == model.selectedEType).Single();
Newevent.Location = model.Location;
if (Request.Files.Count != 0)
{
string fileName = Guid.NewGuid().ToString();
string serverPath = Server.MapPath("~\\Content\\EventPicture");
Bitmap newImage = new Bitmap(Request.Files[0].InputStream);
newImage.Save(serverPath + "\\" + fileName + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
model.eventsImage = "Content/EventPicture/" + fileName + ".jpg";
Newevent.FestivalID = model.festivalID = festID;
db.Events.Add(Newevent);
db.SaveChanges();
//Change the model.festivalID to Newevent.FestivalID
return RedirectToAction("Details", "Festival", new { id = Newevent.FestivalID });
}
else
{
db.Events.Add(Newevent);
db.SaveChanges();
//Change the model.festivalID to Newevent.FestivalID
return RedirectToAction("Details", "Festival", new { id = Newevent.FestivalID });
}
}
ModelState.AddModelError("", "No Event Type Picked");
}
model.eType = db.EType.ToDictionary(p => p.ID, q => q.EType);
model.eType.Add(-1, "----- Add New Event Type -----");
model.eventsDate = DateTime.Now;
model.startTime = DateTime.Now;
model.endTime = DateTime.Now;
return View(model);
}
这是我的模特
public class Events
{
[Required]
public int ID { get; set; }
[Required]
public int FestivalID { get; set; }
[Required(ErrorMessage="Please input the Event Name.")]
[Display(Name = "Event name"), StringLength(100)]
[DisplayFormat(ApplyFormatInEditMode = true)]
public string EventsName { get; set; }
[Display(Name = "Event date")/*, DataType(DataType.Date)*/]
[DisplayFormat(DataFormatString = "{0:dd-MMM-yyyy}", ApplyFormatInEditMode = true)]
[Required(ErrorMessage = "Please input the Event's Date.")]
public DateTime EventsDate { get; set; }
[Display(Name = "Start Time"), DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
[Required(ErrorMessage = "Please input the Event's Start Time.")]
public DateTime StartTime { get; set; }
[Display(Name = "End Time"), DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
[Required(ErrorMessage = "Please input the Event's end time.")]
public DateTime EndTime { get; set; }
[Required]
[Display(Name = "Location")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public string Location { get; set; }
[Required]
[Display(Name = "Event Type")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public virtual EventType EType { get; set; }
[Display(Name = "Event Logo")]
[DataType(DataType.Upload)]
[DisplayFormat(ApplyFormatInEditMode = true)]
public string EventLogo { get; set; }
}
视图模型
public class EventsVM
{
[Required]
[Display(Name = "Event Type")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public Dictionary<int, string> eType { get; set; }
public int selectedEType { get; set; }
[Required]
public int ID { get; set; }
[Required]
public int festivalID { get; set; }
[Required]
[Display(Name = "Event Name"), StringLength(100)]
[DisplayFormat(ApplyFormatInEditMode = true)]
public string EventsName { get; set; }
[Required]
[Display(Name = "Event Date")/*, DataType(DataType.Date)*/]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime eventsDate { get; set; }
[Display(Name = "Start Time"), DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
[Required(ErrorMessage = "Please input the Event's Start Time.")]
public DateTime startTime { get; set; }
[Display(Name = "End Time"), DataType(DataType.Time)]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:hh:mm tt}")]
[Required(ErrorMessage = "Please input the Event's end time.")]
public DateTime endTime { get; set; }
[Required]
[Display(Name = "Location")]
[DisplayFormat(ApplyFormatInEditMode = true)]
public string Location { get; set; }
public HttpPostedFileWrapper imageFile { get; set; }
[Display(Name="Event Image")]
public string eventsImage { get; set; }
}
这是我的观点
@model MyFestival.Models.EventsVM
@{
ViewBag.Title = "Create Event";
Layout = "~/Views/Shared/Festival.cshtml";
}
<h2>Add an event for #@Model.festivalID</h2>
@using (Html.BeginForm())
{
@*@using (Html.BeginForm("Create2", "Events", FormMethod.Post, new {enctype="multipart/form-data"}))
{*@
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.EventsName, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-pencil"></i></span>
<input class="form-control" id="EventsName" required="required" style="width: 210px" name="EventsName" placeholder="Please enter event name" />
@*@Html.EditorFor(model => model.EventsName, new { @class = "form-control", @style = "width:250px" })*@
</div>
@Html.ValidationMessageFor(model => model.EventsName, null, new { @style = "color:red;" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.eventsDate, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-calendar"></i></span>
@Html.TextBoxFor(model => model.eventsDate, new { @class = "form-control datepicker", @style = "width:210px" })
</div>
@Html.ValidationMessageFor(model => model.eventsDate, null, new { @style = "color:red;" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.startTime, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
<input id="startTime" name="startTime" required="required" class="form-control bootstrap-timepicker" style="width: 210px" />
@*@Html.EditorFor(model => model.startTime, new { @class = "form-control", @style = "width:250px" })*@
</div>
@*@Html.EditorFor(model => model.startTime, new { @class = "form-control" })*@
@Html.ValidationMessageFor(model => model.startTime, null, new { @style = "color:red;" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.endTime, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class='input-group date' id='datetimepicker4'>
<span class="input-group-addon"><i class="glyphicon glyphicon-time"></i></span>
<input name="endTime" id="endTime" type='text' class="form-control bootstrap-timepicker" style="width: 210px" />
@*@Html.TextBoxFor(model => model.startDate, new { @class = "form-control datepicker", @style = "width:210px" })*@
</div>
@Html.ValidationMessageFor(model => model.endTime, null, new { @style = "color:red;" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.eType, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-tag"></i></span>
@Html.DropDownListFor(p => p.selectedEType, Model.eType.Select(p => new SelectListItem() { Text = p.Value.ToString(), Value = p.Key.ToString(), Selected = false }), new { @class = "form-control", @style = "width:210px", @onchange = "checkaddnew();" })
</div>
@Html.ValidationMessageFor(model => model.eType, null, new { @style = "color:red;" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Location, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Location, new { @style = "width:300px;", @class = "form-control", @rows = "3" })
@Html.ValidationMessageFor(model => model.Location, null, new { @style = "color:red;" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.eventsImage, new { @class = "control-label col-md-2" })
<div class="col-md-10">
<input name="imageFile" id="File" type="file" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-info" />
@Html.ActionLink("Back to List", "Index", "Festival", null, new { @class = "btn btn-danger" })
</div>
</div>
</div>
}
@Html.Partial("CreateEventType", new MyFestival.Models.EventTypeVM())
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
<script>
$(document).ready(function () {
$('#selectedEType').change(function () {
if ($(this).find(":selected").val() == -1) {
$('#myModal').modal('show');
}
});
});
</script>
<script type="text/javascript">
function ajaxResponse(data) {
alert("This Worked and the Data ID is: " + data.EventTypeID);
var newOption = "<option value='" + data.EventTypeID + "'>" + data.Name + "</option>";
$('#selectedEType').append(newOption);
$('#myModal').modal('hide');
$("#selectedEType option[value='" + data.EventTypeID + "']").attr("selected", "selected");
};
</script>
<script type="text/javascript">
$(document).ready(function () {
$("#eventsDate").datepicker('setDate', '+1',{dateFormat: "dd/mm/yy"}).on('changeDate', function (ev) {
$(this).blur();
$(this).datepicker('hide');
});
});
</script>
<script type="text/javascript">
$('#startTime').timepicker({
minuteStep: 5,
showInputs: false,
disableFocus: true
});
</script>
<script type="text/javascript">
$('#endTime').timepicker({
minuteStep: 5,
showInputs: false,
disableFocus: true
});
</script>
}
答案 0 :(得分:2)
在您的GET控制器中,您要在模型中设置festivalID:
EventsVM events = new EventsVM { festivalID = festID };
所以在你的视图形式的某个地方(我的偏好将在你的ValidationSummary之下),添加
@Html.HiddenFor(m => m.festivalID)
然后你不需要POST控制器中的第二个参数:
public ActionResult Create2(EventsVM model)
因为模型通过model.festivalID
包含festivalID。