【问题标题】:How do I use same view model but for different view excluding the required properties?我如何使用相同的视图模型,但不同的视图不包括所需的属性?
【发布时间】:2018-05-08 12:08:19
【问题描述】:

我有一个视图 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-->
}

【问题讨论】:

  • 您不能(除非您使用基于模型中另一个属性的条件[RequiredIf] 验证属性)。但是你总是可以创建一个具有公共属性的 BaseViewModel,然后具体的视图模型继承自添加其他 2 个属性([Required] 属性在一个,但不是另一个)
  • Bind(Include) 是做什么用的?
  • 当你有视图模型时它是不合适的(你已经被保护免受过度发布攻击) - 但它指定了哪些属性从绑定中包含/排除 - 所以它不适合你的情况(你做不想排除属性)

标签: c# asp.net asp.net-mvc-4 viewmodel


【解决方案1】:

我想快速而肮脏的方法是使用 @Html.Hiddenfor 并使用控制器内部的新日期时间填充值

【讨论】:

    【解决方案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; }
        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。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      相关资源
      最近更新 更多