繁体   English   中英

如何从aspx页面中的asp.net c#中的表单发送两个对象

[英]How to send two objects from a form in asp.net c# in an aspx page

我需要使用绑定将两个对象发送到控制器,但是我不知道该怎么做。 我只知道如何发送一个对象而不是多个对象。

这是我如何使用单个对象的示例。

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<Inventario.Models.Report>" %>

<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Create
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">

<h2>Create</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>Report</legend>

        <div class="editor-label">
            <%: Html.LabelFor(model => model.Client, "Client_1") %>
        </div>
        <div class="editor-field">
            <%: Html.DropDownList("Client", String.Empty) %>
            <%: Html.ValidationMessageFor(model => model.Client) %>
        </div>



        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
<% } %>

<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>

</asp:Content>

然后,它被控制器以这种方式接收:

[HttpPost]
public ActionResult Create(Report report)
{
    //Some code here
}

我需要得到两个对象。 IDK可能是这样的:

[HttpPost]
public ActionResult Create(Report report, AnotherObject ao)
{
    //Some code here
}

我是一个初学者。 您可以给我的任何指南都会对我有很大帮助。 预先感谢。

您必须创建一个这样的ViewModel:

public class MyViewModel
{
 public Report report {get;set;} 
 pulic AnotherObject ao {get;set;}
}

从您的“采取行动”中传递给他们:

public ActionResult Create()
{
   MyViewModel model = new MyViewModel();
   model.report = new Report();
   model.ao = new AnotherObject();

   return View(model);
}

在您的视图中将Model设置为MyViewModel

并将其发布到行动中:

[HttpPost]
public ActionResult Create(MyViewModel myModel)
{
    //Some code here
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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