我有一个视图AddAppointment的视图模型。它有许多属性,其中2个是必需(我在其上写了必需属性)。
现在我想对另一个视图使用相同的模型,但不包括所需的属性,但它不起作用,即它无效。
除了编写另一个视图模型外,该怎么做?
查看型号:
public class AddBookingsViewModel
{
public string CustomerName { get; set; }
public string ContactNo { get; set; }
public string VehicleRegNo { get; set; }
public short fk_VehicleMakeID { get; set; }
public string VehicleModel { get; set; }
[Required(ErrorMessage = "Select appointment time ")]
public int fk_TimeSlotID { get; set; }
public byte fk_BookingModeID { get; set; }
public int EntryUserID { get; set; }
public int ReturnBookingID { get; set; }
[Required(ErrorMessage="Fill in the appointment date")]
[DataType(DataType.Date)]
public DateTime? AppointmentDate { get; set; }
}
查看:(使用地点)
@model ZahidCarWash.ViewModels.AddBookingsViewModel
@{
ViewBag.Title = "Add Appointment";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<!-- page banner -->
<!-- end page banner -->
@using (Html.BeginForm())
{
<!-- appointments -->
<div id="appointments" class="appointment-main-block appointment-two-main-block">
<div class="container">
<div class="row">
<div class="section text-center">
<h3 class="section-heading text-center">Get an Appointment</h3>
</div>
<div class="col-md-8 col-sm-12">
<div class="appointment-block">
<h5 class="form-heading-title"><span class="form-heading-no">1.</span>Vehicle Information</h5>
<div class="row">
<div class="col-sm-4">
<div class="dropdown">
@Html.DropDownListFor(Model => Model.fk_VehicleMakeID, new SelectList(ZahidCarWash.DAL.VehicleMakesRepository.getVehicleMakes(), "VehicleMakeID", "MakeTitle"),
new { @class = "form-control" })
</div>
</div>
<div class="col-sm-4">
@Html.EditorFor(Model => Model.VehicleModel, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Vehicle Model" } })
</div>
<div class="col-sm-4">
@Html.EditorFor(Model => Model.VehicleRegNo, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Vehicle Reg No." } })
</div>
</div>
<h5 class="form-heading-title"><span class="form-heading-no">2.</span>Contact Details</h5>
<div class="row">
<div class="col-sm-4">
@Html.EditorFor(Model => Model.CustomerName, new { htmlAttributes = new { @class = "form-control", placeholder = "Customer Name" } })
@Html.ValidationMessageFor(Model => Model.CustomerName, "", new { @class = "ErrorMessages" })
</div>
<div class="col-sm-4">
@Html.EditorFor(Model => Model.ContactNo, new { htmlAttributes = new { @class = "form-control", placeholder = "Enter Contact Number." } })
@Html.ValidationMessageFor(Model => Model.ContactNo, "", new { @class = "ErrorMessages" })
</div>
</div>
<button type="submit" class="btn btn-default pull-right">Book Now</button>
</div>
</div>
</div>
</div>
</div>
}
控制器:
public JsonResult AddManualAppointment(AddBookingsViewModel AddBookingVM)
{
if (ModelState.IsValid)
{
AddBookingVM.fk_BookingModeID = 2;
int ReturnRowsCount = BookingRep.InsertCustomerAppointments(AddBookingVM, out ReturnStatus, out ReturnMessage, out ReturnBookingID);
}
else
{
}
return Json(new { ReturnMessageJSON = ReturnMessage, ReturnStatusJSON = ReturnStatus });
}
数据通过ajax传递:
<script type="text/javascript">
//to add an appointment
$('form').submit(function (e) {
e.preventDefault();
if (!$(this).valid()) {
return;
}
var url = '@Url.Action("AddManualAppointment")';
var data = $(this).serialize();
$.post(url, data, function (response) {
if (response.ReturnStatusJSON == true) {
swal("Booked !", response.ReturnMessageJSON, "success");
$("#VehicleRegNo").val("");
$("#VehicleModel").val("");
$("#CustomerName").val("");
$("#ContactNo").val("");
}
else {
swal("Sorry !", response.ReturnMessageJSON, "error");
}
});
});
</script>
<!--End Custom Scripts-->
}
答案 0 :(得分:0)
我想快速而肮脏的方法是使用@ Html.Hiddenfor并使用控制器内部的新日期时间填充值
答案 1 :(得分:0)
您可以使用继承将视图模型拆分为包含和不包含必需属性的版本:
public class AddBookingsViewModel
{
public string CustomerName { get; set; }
public string ContactNo { get; set; }
public string VehicleRegNo { get; set; }
public short fk_VehicleMakeID { get; set; }
public string VehicleModel { get; set; }
public byte fk_BookingModeID { get; set; }
public int EntryUserID { get; set; }
public int ReturnBookingID { get; set; }
}
public class AddBookingsViewModelWithAppointment : AddBookingsViewModel
{
[Required(ErrorMessage = "Select appointment time ")]
public int fk_TimeSlotID { get; set; }
[Required(ErrorMessage="Fill in the appointment date")]
[DataType(DataType.Date)]
public DateTime? AppointmentDate { get; set; }
}
这允许您在您的情况下使用适当的视图模型,并通过多态保持兼容性。
如果您需要基类中的可选属性,可以将属性设置为虚拟并在派生类中应用该属性:
public class AddBookingsViewModel
{
public string CustomerName { get; set; }
public string ContactNo { get; set; }
public string VehicleRegNo { get; set; }
public short fk_VehicleMakeID { get; set; }
public string VehicleModel { get; set; }
public byte fk_BookingModeID { get; set; }
public int EntryUserID { get; set; }
public int ReturnBookingID { get; set; }
public virtual int fk_TimeSlotID { get; set; }
public virtual DateTime? AppointmentDate { get; set; }
}
public class AddBookingsViewModelWithAppointment : AddBookingsViewModel
{
[Required(ErrorMessage = "Select appointment time ")]
public override int fk_TimeSlotID {
get => base.fk_TimeSlotID;
set => base.fk_TimeSlotID = value;
}
[Required(ErrorMessage="Fill in the appointment date")]
[DataType(DataType.Date)]
public override DateTime? AppointmentDate {
get => base.AppointmentDate;
set => base.AppointmentDate = value;
}
}
使用最适合您业务案例的veriant。