mvc3 模式下多对象提交问题

jishengzu 2013-03-23 09:34:02

public ActionResult Edit(int id)
{
这里需要提交employee
和list<employeetype>
}
如果不封装成一个对象可以在view中显示2个值么

在view中提交后怎么在
[HttpPost]
public ActionResult Edit(int id, Employee obj)
{
这里获得提交的employeetype值
}

不知道我这样描述清不清晰
就是一个Action处理多个对象
一个view显示提交多个对象

...全文
186 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
jishengzu 2013-03-23
  • 打赏
  • 举报
回复

  public ActionResult Edit(int id)
        {
            Employee eObj = EmployeeService.GetSObj.SelectById(id);
            if (eObj != null)
            {
                ViewBag.list = EmployeeTypeService.GetSObj.Select()
                    .Select(c =>
                    new SelectListItem{
                               Text=c.Name,
                               Value=c.Id.ToString()
                               });             
                return View(eObj);
            }
            else
                ModelState.AddModelError("", "该记录无效。");
            return RedirectToAction("List");
        }

 [HttpPost]
        public ActionResult Edit(int id, Employee obj, string select)
        {
            NotNullObj(obj);

            if (ViewBag.TypeList!=null)
            {
            }
            try
            {
                EmployeeService.GetSObj.Update(obj);
                // TODO: Add update logic here

                return RedirectToAction("List");
            }
            catch
            {
                return View();
            }
        }
视图

@model AlinqCoreProject.Model.Employee
@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>
    Edit</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm())
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Employee</legend>
        @Html.HiddenFor(model => model.Id)
        <div class="editor-label">
            @Html.LabelFor(model => model.Code)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Code)
            @Html.ValidationMessageFor(model => model.Code)
        </div>
        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <div class="editor-label">
            员工类型
        </div>
        <div class="editor-field">
           @Html.DropDownList("select", (IEnumerable<SelectListItem>)ViewBag.list)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}
<div>
    @Html.ActionLink("Back to List", "Index")
</div>

这样就行了
我是老姚 2013-03-23
  • 打赏
  • 举报
回复
public ActionResult Edit(int id) { ViewDate["employee"] = employee //这里需要提交employee ViewDate["employeetype"] = listEmployeeType;//和list<employeetype> } 如果不封装成一个对象可以在view中显示2个值么 在view中提交后怎么在 [HttpPost] public ActionResult Edit(int id, Employee obj) { 这里获得提交的employeetype值//employeetype如果是Employee的属性的话可以是可以直接封装在obj对象中的,所以obj的employeetype直接通过obj.EmployeeType应该可以取到。 }

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧