简体   繁体   English

MVC5:视图未提交正确的ViewModel

[英]MVC5 : View not submitting correct ViewModel

Minimal Code Structure 最小的代码结构

I have two ViewModels cmsViewModel and appointments 我有两个ViewModels cmsViewModelappointments

public partial class cmsViewModel
    {
        public string rows { get; set; }
        public DateTime appt_date_time { get; set; }
        public int appt_id { get; set; }
        public List<community_meeting_schedule> lst { get; set; }

        public cmsViewModel()
        {
            rows = null;
            appt_date_time = DateTime.Today;
            lst = null;
            appt_id = -1;

        }

    }



public partial class appointments
    {

        public int appt_client_id { get; set; }
         public int customer_id { get; set; }
        [Key]
        public int appt_id { get; set; }

        public DateTime appt_date_time { get; set; }
        public DateTime time_stamp { get; set; }
    }

The action method in controller looks like: 控制器中的操作方法如下所示:

 [HttpPost]
        public ActionResult CMSConfim(cmsViewModel model, string Command)
        {

            return View("CMSchedule", model);

        }

The View CMSConfim.cshtml looks like below: View CMSConfim.cshtml如下所示:

@model  Scheduler_MVC.Models.cmsViewModel
@using (Html.BeginForm("CMSConfim", "appointments", FormMethod.Post))

{


    @Html.HiddenFor(model => model.appt_id, new { id = "appt_id" })
    @Html.HiddenFor(model => model.appt_client_id, new { id = "appt_client_id" })
    @Html.HiddenFor(model => model.appt_date_time)
    @Html.HiddenFor(model => model.appt_status)
    @Html.HiddenFor(model => model.appt_type)
    @Html.HiddenFor(model => model.lst)

        <input type="submit" value="Back" id="backBtn" class="btn btn-default" />
        <input type="button" value="Submit" id="submitBtn" class="btn btn-default" style="display:inline-block;" />


}

I would like to add that I am able to render correct values in the display fields on the form. 我想补充一点,就是我能够在表单的显示字段中呈现正确的值。 The error comes while submitting the form. 提交表单时出现错误。

Error 错误

Now when I submit the form through Back . 现在,当我通过Back提交表单时。 I get the following error. 我收到以下错误。 The model in the dictionary is of type 'cmsViewModel' but required is that of type 'appointments' Please suggest what I might be doing wrong. 字典中的模型类型为“ cmsViewModel”,但必需的类型为“ appointments”。请提出我可能做错了的事情。

Your post view model type is "appointments", but your Html.BeginForm is also routing to an "AppointmentsController" 您的帖子视图模型类型为“约会”,但您的Html.BeginForm也将路由到“ AppointmentsController”

@using (Html.BeginForm("CMSConfim", "appointments", FormMethod.Post))

This route is expecting the following 这条路线期待以下

public class AppointmentsController : Controller
{
    public Action CMSConfirm(appointments model) 
    {

    }
}

Update your Html.BeginForm if the controller name is wrong 如果控制器名称错误,请更新您的Html.BeginForm

I don't see a match for parameter "Command" either. 我也看不到参数“ Command”的匹配项。

Why do you have in your razor sintax cmsViewModel? 为什么您的剃须刀sintax cmsViewModel中有? You expect to submit appointmets but there is cmsVM. 您希望提交约会,但是有cmsVM。 Also in your controller you are expecting cmsVM. 同样在您的控制器中,您期望使用cmsVM。 You need to provide appointmentsVM in razor and also to expect it in your Controller. 您需要在剃须刀中提供约会VM,并在Controller中期待它。

    [HttpPost]
     public ActionResult CMSConfim(appointments model, string Command)
    {

        return View("CMSchedule", model);

    }

Or if you want to get both in controller. 或者,如果您想同时使用控制器。

  [HttpPost]
     public ActionResult CMSConfim(cmsViewModel  model, appointments appoint, string Command)
    {

        return View("CMSchedule", model);

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM