MVC5 : View not submitting correct ViewModel

时间:2016-07-11 20:51:45

标签: c# asp.net razor mvvm asp.net-mvc-5

Minimal Code Structure

I have two ViewModels cmsViewModel and appointments

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:

@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. 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.

2 个答案:

答案 0 :(得分:1)

您的帖子视图模型类型是“约会”,但您的Html.BeginForm也路由到“AppointmentsController”

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

这条路线期待以下

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

    }
}

如果控制器名称错误,请更新您的Html.BeginForm

我也没有看到参数“Command”的匹配。

答案 1 :(得分:0)

Why do you have in your razor sintax cmsViewModel? You expect to submit appointmets but there is cmsVM. Also in your controller you are expecting cmsVM. You need to provide appointmentsVM in razor and also to expect it in your 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);

    }