【问题标题】:ASP.NET MVC - Razor: Assign a different model to BeginFormASP.NET MVC - Razor:为 BeginForm 分配不同的模型
【发布时间】:2020-10-02 04:19:52
【问题描述】:

假设我有一个显示产品信息的页面。模型看起来像这样:

public class Product
{
    public int ProductID {get; set; }
    public string Description {get; set; }
    public bool IsAvailable {get; set; }
}

我的页面顶部有@model MyApp.Models.Product,一切正常。

现在我想在我的页面底部添加一个表单,让用户可以发送邮件询问有关产品的更多信息。

我有一个带有RequestInfo 操作的控制器,它向网站所有者发送电子邮件,以此为模型:

public class InfoRequest
{
    /* ... */
    public int ProductID { get; set; }
    public string CustomerName { get; set; }
    public string CustomerPhone {get; set; }
    public string Message {get; set; }
    /* ... */
}

问题是,如果我使用Html.BeginForm 创建一个表单,每个@Html.TextBoxFor 都引用Product 模型,我不知道如何指定不同的模型。

我在其他帖子中发现,我可以通过创建以 InfoRequest 为模型的局部视图来解决这些问题。

有没有不使用局部视图的方法来解决?

如果局部视图是唯一的方法,是否可以在页面内定义局部视图(即不使用外部文件)?

谢谢!

【问题讨论】:

  • 我将创建一个新模型,列出 Product 类和 InfoRequest 类,然后在您的视图中选择每个项目。
  • 我的回答有帮助吗?

标签: asp.net-mvc razor razor-pages


【解决方案1】:

您也许可以通过使用纯 html 来做到这一点,但是,我不确定这是否可行,您可能需要编辑输入的名称属性,例如Name="Model.CustomerName" 而不是 Name="CustomerName"

@Html.BeginForm("actionName", "controllerName", FormMethod.Post, new { @class = "form" })
{ 
<div class="form-group">
   <label class="control-label" for="CustomerName">Name</label>
   <input class="form-control" name="CustomerName" id="CustomerName">
</div>
<div class="form-group">
   <label class="control-label" for="CustomerPhone">Phone</label>
   <input class="form-control" name="CustomerPhone" id="CustomerPhone">
</div>
<div class="form-group">
   <label class="control-label" for="Message">Message</label>
   <textarea class="form-control" name="Message" id="Message"></textarea>
</div>
<div class="form-group">
   <input class="btn btn-success" type="submit">Send</input >
</div>
}

不过,另一种方法是创建一个包含两个模型的视图模型。 第 1 步。创建一个列出这两个类的新模型。

public class ProductsInfoVM
{
   public List<Product> Products { get; set; }
   public List<InfoRequest> InfoRequests { get; set; }
}

第 2 步。创建视图:

<table class="table">
 <tr>
   <th>
      @Html.DisplayNameFor(model => model.Products[0].InfoRequest.CustomerName)
   </th>
   <th>
      @Html.DisplayNameFor(model => model.Products[0].Description)
   </th>
   <th>
      @Html.DisplayNameFor(model => model.Products[0].IsAvailable)
   </th>
   <th></th>
 </tr>

@foreach (var item in Model.Products)
{
 <tr>
   <td>
       @Html.DisplayFor(modelItem => item.InfoRequest.CustomerName)
   </td>
   <td>
       @Html.DisplayFor(modelItem => item.Description)
   </td>
   <td>
       @Html.DisplayFor(modelItem => item.IsAvailable)
   </td>
   <td>
      @Html.ActionLink("Edit", "Edit", new { id = item.ProductId }) |
      @Html.ActionLink("Details", "Details", new { id = item.ProductId }) |
      @Html.ActionLink("Delete", "Delete", new { id = item.ProductId })
   </td>
 </tr>
}
</table>

表格:

@Html.BeginForm("actionName", "controllerName", FormMethod.Post, new { @class = "form" })
{
    <div class="form-group">
        @Html.LabelFor(x => x.InfoRequests[0].CustomerName, new { @class="control-label" })
        @Html.TextBoxFor(x => x.InfoRequests[0].CustomerName, new { @class = "form-control" })
        @Html.ValidationMessageFor(x => x.InfoRequests[0].CustomerName, "Your name cannot be blank" ,new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(x => x.InfoRequests[0].CustomerPhone, new { @class = "control-label" })
        @Html.TextBoxFor(x => x.InfoRequests[0].CustomerPhone, new { @class = "form-control" })
        @Html.ValidationMessageFor(x => x.InfoRequests[0].CustomerPhone, "Your phone cannot be blank", new { @class = "form-control" })
    </div>
    <div class="form-group">
        @Html.LabelFor(x => x.InfoRequests[0].Message, new { @class = "control-label" })
        @Html.TextBoxFor(x => x.InfoRequests[0].Message, new { @class = "form-control" })
        @Html.ValidationMessageFor(x => x.InfoRequests[0].Message, "Your message cannot be blank", new { @class = "form-control" })
    </div>
    <div class="form-group">
        <input class="btn btn-success" type="submit">Send</input >
    </div>
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-27
    • 2015-12-09
    • 1970-01-01
    相关资源
    最近更新 更多