如何仅从 asp.net mvc 中的主模型发送视图模型?

How to send View Model only from Main Model in asp.net mvc?

我的 MVC 使用 HTML.BeginForm Form.Here 是 html 代码


@model TodayViewModel
@{
    ViewBag.Title = "Work Completed Today";
    Layout = "~/Views/Shared/_LayoutBackend.cshtml";
}

 @using (Html.BeginForm("Create", "CustomActivity", FormMethod.Post, new { role = "form", @id = "customActivityForm", @class = "activityForm" }))
                    {
                        <div>
                            <div class="myClass">
                            </div>

                            <div class="container" id="workordercategories">
                                @Html.LabelFor(m => m.CustomActivity.WorkOrderCategoriesName, new { @class = "" })
                                @Html.TextBoxFor(m => m.CustomActivity.WorkOrderCategoriesName, new { @class = "w100p mb0" })
                                @Html.ValidationMessageFor(m => m.CustomActivity.WorkOrderCategoriesName, "", new { @class = "text-danger" })
                            </div>


                            <div class="container" id="activity">
                                @Html.LabelFor(m => m.CustomActivity.Activity, new { @class = "" })
                                @Html.TextBoxFor(m => m.CustomActivity.Activity, new { @class = "w100p mb0" })
                                @Html.ValidationMessageFor(m => m.CustomActivity.Activity, "", new { @class = "text-danger" })
                            </div>
                        </div>
                     }

我的视图模型是

  public class TodayViewModel
    {
        public IList<TodayListViewModel> TodaysVM { get; set; }
        public CustomActivityViewModel CustomActivity { get; set; }

    }

 public class CustomActivityViewModel
    {
        [Required, Display(Name = "Work Category Name")]
        public string WorkOrderCategoriesName { get; set; }
        [Required]
        public string Activity { get; set; }
    }

提交表单时控制器方法是:

[HttpPost]
        public ActionResult Create(TodayViewModel model)
        {
            // to do here
        }

在控制器方法中,我必须使用 TodayViewModel,其中有两种方法,其中一种 (TodaysVM) 始终为空。有什么方法可以提交表单,以便我可以在 Controller 中使用 CustomActivityViewModel 而不是 TodayViewModel?现在,如果我使用 CustomActivityViewModel,控制器中的值为 null。

如果不能在视图中使用CustomActivityViewModel试试

[HttpPost]
public ActionResult Create(TodayViewModel model)
{
     CustomActivityViewModel obj = model.CustomActivity;

     //....
}