【问题标题】:How do I pass an entire object using an HTML form?如何使用 HTML 表单传递整个对象?
【发布时间】:2022-01-05 05:14:15
【问题描述】:

为简单起见,我将只包括模型的相关属性和相关行:

型号:Department.cs
属性:public virtual Staff HOD { get; set; }

控制器方法:

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Add([Bind] Department newDepartment)
{
    await _departmentRepository.AddDepartmentAsync(newDepartment);
    await _departmentRepository.SaveTransactionAsync(true);
    return Redirect("Details" + "/" + newDepartment.Id);
}

查看:

@model Department
@inject IStaffRepository staffRepository

@{
    // select list
    var listOfEmployees = new List<Staff>();
    foreach (var staff in await staffRepository.GetAllStaffAsync())
    {
        listOfEmployees.Add(staff);
    }
    var selectListHOD = new SelectList(listOfEmployees, "EmployeeId", "Name"); //populates the option element with the correct Employee Id
}

<form class="form-group" method="post">
    <div class="d-flex flex-column">
        <div class="input-group" style="margin-bottom: 1%">
            @Html.DropDownListFor(department => department.HOD, selectListHOD, htmlAttributes: new { @class="form-control" })
        </div>
        <div class="input-group">
            <input class="btn btn-primary" type="submit" value="Save" />
        </div>
    </div>
</form>

结果:
(我没有在问题中包含此处看到的其他属性,但请相信我,控制器参数的属性会正确填充它们,HOD 属性除外)


来源:

<form class="form-group" method="post">
    <div class="d-flex flex-column">
        <div class="input-group" style="margin-bottom: 1%">
            <input class="form-control" id="Id" name="Id" placeholder="Id" type="text" value="" /> </div>
        <div class="input-group" style="margin-bottom: 1%">
            <input class="form-control" id="Name" name="Name" placeholder="Department" type="text" value="" /> </div>
        <div class="input-group" style="margin-bottom: 1%">
            <input class="form-control" id="Description" name="Description" placeholder="Description" type="text" value="" /> </div>
        <div class="input-group" style="margin-bottom: 1%">
            <select class="form-control" id="HOD" name="HOD">
                <option value="EMP01">Employee 1</option>
                <option value="EMP02">Employee 2</option>
            </select>
        </div>
        <div class="input-group">
            <input class="btn btn-primary" type="submit" value="Save" /> </div>
    </div>
    <input name="__RequestVerificationToken" type="hidden" value="CfDJ8AIwSWkSO0hJqMxy-oQKoeG35LTDmI2N1XHDZL9qeaxRxc17TyZbT2z9Iq0GMPkRyE7HnaX1r4ZSIs0bQATYB7w_A_HZDBXGETMmpdSqlMXZCmf7cH9ECzrNGz0Wuu9zHkE50yI92vPY-GxNPG-pRhs" /> 
</form>

我的猜测是提供给控制器的值似乎是错误的。我将如何传递实际对象而不是 ID?
我尝试使用this 代替EmployeeId,但它会抛出Object Reference not found 异常。

【问题讨论】:

标签: c# forms object data-binding asp.net-core-mvc


【解决方案1】:

您不能将对象与&lt;select&gt;&lt;/select&gt;。您可以尝试用&lt;select&gt;&lt;/select&gt; 987654326 @ .and添加一个隐藏的输入来绑定Hod.Name。选定的值更改时,更改隐藏的输入值。是一个演示:

<form class="form-group" method="post">
    <div class="d-flex flex-column">
        <div class="input-group" style="margin-bottom: 1%">
            @Html.DropDownListFor(department => department.HOD.EmployeeId, selectListHOD, htmlAttributes: new { @class = "form-control",@onChange= "AddName()" })
            <input hidden asp-for="HOD.Name" />
        </div>
        <div class="input-group">
            <input class="btn btn-primary" type="submit" value="Save" />
        </div>
    </div>
</form>
@section scripts
{
    <script>
        $(function () {
            AddName();
        })
        function AddName() {
            $("#HOD_Name").val($("#HOD_EmployeeId option:selected").text());
        }
    </script>
}

结果:

【讨论】:

  • 为什么我们必须使用 $ 的东西?
  • $ is simply a valid JavaScript identifier.。可以参考link
  • 是的,但是为什么我们首先要写$(function () { AddName(); })?我们不能直接写&lt;script type="text/javascript"&gt; function AddName() { $("#HOD_Name").val($("#HOD_EmployeeId option:selected").text()); } &lt;/script&gt; 吗?
  • $(function () { AddName(); }) 将帮助您在页面加载时设置隐藏输入的值。这样如果您不更改下拉菜单的默认选择值,隐藏输入的值将是默认选择的选项。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-25
  • 1970-01-01
  • 2010-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多