简体   繁体   中英

MVC5 : View not submitting correct ViewModel

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.

Your post view model type is "appointments", but your Html.BeginForm is also routing to an "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

I don't see a match for parameter "Command" either.

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);

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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