如何解决如何仅从asp.net mvc中的主模型发送视图模型?
我已经将HTML.BeginForm用于我的MVC表单。这里是html代码
arr = [18,4,6]
count = 0
for i in range(len(arr):
if arr[i] != 0:
arr[i],arr[count] = arr[count],arr[i]
count += 1
我的视图模型是
@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
始终为空。有什么方法可以提交表单,以便我可以在Controller中使用(TodaysVM)
而不是 CustomActivityViewModel
吗?现在,如果我使用CustomActivityViewModel,则控制器中的值为null。
解决方法
如果您无法在View中使用CustomActivityViewModel
,请尝试
[HttpPost]
public ActionResult Create(TodayViewModel model)
{
CustomActivityViewModel obj = model.CustomActivity;
//....
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。