c# - 在 MVC 中编辑列表集合中的属性的方法

标签 c# asp.net-mvc

    public class VerifyLicensorModel
    {
        public int TempId { get; set; } 
        public string Licensor { get; set; } 
        public string Address { get; set; }
        public int LicensorId { get; set; }
        public int ActionId { get; set; }   
        public int ReferenceId { get; set; }
    }

这是我的模型,我通过 excel 导入得到了这个列表,我要求用户验证数据

因此对于每个许可方用户都应该选择一个操作 [actionId] 并且还有一个基于用户选择的操作 [ReferenceId] 的引用 因此,为了选择 ActionId 和 ReferenceId,我使用了一个模式弹出窗口,它允许用户选择操作和 referenceID [决定这 2 个参数的过程很复杂,包括很多条件检查和输入的并行选择, 示例 首先决定 liceonsor 是新的还是现有的。如果决策存在,则查找现有的 id 和相关地址。然后决定使用新地址还是替换现有地址。如果决定替换现有的,请选择要替换的

这就是为什么计划将其逻辑保持为单独的逻辑并仅返回结果,即 2 IDs actionid 和 referenceID]

提交时我如何设置这两个属性的值 我的观点我是这样计划的

        @model  IList<ApplicationLab.Models.VerifyLicensorModel>
        @using (Html.BeginForm())
        {
            @Html.ValidationSummary(true)

            <table class="table">
                <tr>
                    <th>
                      Licensor
                    </th>
                    <th>
                     Address
                    </th>
                    <th>
                        Action
                    </th>
                    <th>
                        Verify
                    </th>
                </tr>
              @for (int i = 0; i < Model.Count(); i++) 
              {
                    <tr>
                        <td>
                            @Html.HiddenFor(modelItem => Model[i].TempId)
                            @Html.HiddenFor(modelItem => Model[i].LicensorId)
                            @Html.DisplayFor(modelItem => Model[i].Licensor)                                
                        </td>
                        <td>
                            @Html.TextAreaFor(modelItem => Model[i].Address)
                        </td>
                        <td>                                  
                            @Html.HiddenFor(modelItem => Model[i].ActionId)
                            @Html.HiddenFor(modelItem => Model[i].ReferenceId)                             
                        </td>
                        <td>
                            <a >Verify</a> // onclick plannig to show popup
                        </td>
                    </tr>
                }
            </table>

            <div class="form-group">
                <div class="col-md-10" style="text-align:center;">
                    <input type="submit" value="Submit" class="btn btn-primary" />
                    @Html.ActionLink("Cancel", "LicenseImport", "Import", new { @id = @ViewBag.EditionId }, new { @class = "btn btn-link" })
                </div>
            </div>
        }

那么我的做法对吗?如果是这样,我如何设置选定的行属性? 或者是否有更好的方法或示例来建议实现相同的方法?

最佳答案

通常,您的 View 模型应该代表页面和页面上的元素。如果要在模式上使用它,您可以使用局部 View 模板。

您需要创建 List 的新属性来表示可用于 ActionIds 和 ReferenceIds 的选项,并作为参数传递以在 View 中的辅助函数 @Html.DropDownListFor 中使用

public class VerifyLicensorModel
{
    public int TempId { get; set; } 
    public string Licensor { get; set; } 
    public string Address { get; set; }
    public int LicensorId { get; set; }
    public int ActionId { get; set; }
    public List<SelectListItem> ActionOptions { get; set; }   
    public int ReferenceId { get; set; }
    public List<SelectListItem> ReferenceOptions { get; set; }   
}

在 View 上:

@Html.DropDownListFor(m => m.ActionId, Model.ActionOptions);
@Html.DropDownListFor(m => m.ReferenceId, Model.ReferenceOptions);

关于c# - 在 MVC 中编辑列表集合中的属性的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32112447/

相关文章:

c# - C#中可以动态添加属性吗?

c# - 将此字符串转换为分钟数的最佳方法是什么?

c# - 匈牙利字符的邮件问题

c# - 如何让我的应用滚动经过键盘覆盖的内容

c# - 如何模拟 OperationContext.Current(WCF 消息)

jquery - 使用 asp.net core 和 Jquery 从 ajax 请求将对象绑定(bind)到 asp mvc Controller

asp.net-mvc - 如何在不重复代码的情况下实现业务和 View 验证?

c# - PagesIndex - 对象引用未设置为对象的实例

javascript - 需要将服务/工厂存储为值

javascript - 使用 Jquery 选中/取消选中多个复选框