C#MVC - 控制器未从ViewModel表单接收所有数据

时间:2018-04-18 15:01:14

标签: c# model-view-controller

我正在使用c#MVC,这是我的第一个项目/测试。

我正在使用C#MVC制作一个简单的MVC项目。我的问题是,当我从视图中提交表单时,控制器正在接收部分数据,如果我从PostMan执行相同的帖子,则控制器会收到所有这些信息。

这是我的代码:

查看:

<form asp-action="NewTask" asp-controller="Task" class="form-horizontal" name="NewTask">
<div class="table">
    <dl>
        <dt>
            Title:
        </dt>
        <dd>
            <input class="form-control" name="Title" id="Title" type="text" placeholder="Title" />
        </dd>
        <dt>
            Description:
        </dt>
        <dd>
            <input class="form-control" name="Description" id="Description" type="text" placeholder="Description" />
        </dd>
        <dd>
            <input type="hidden" name="Status" id="Status" value="A" />
        </dd>
        <dt>
            User:
        </dt>
        <dd>
            @Html.DropDownListFor(task => task.User, new SelectList(ViewBag.Users, "Id", "Username"), "Please Select One...", new { @class = "form-control", @name = "User"})                                
        </dd>
    </dl>
</div>
<div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <input type="submit" class="btn btn-primary" id="btnSubmit" />
</div>

视图模型:

public class CreateTaskViewModel
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string Status { get; set; }
    public string User { get; set; }
}

控制器:

[HttpPost, ValidateAntiForgeryToken]
    public IActionResult NewTask([FromBody] CreateTaskViewModel TaskVM)
    {
        var item = new Models.Task
        {
            Title = TaskVM.Title,
            Description = TaskVM.Description,
            Status = TaskVM.Status
        };

        var users = _userRepository.Find(Convert.ToInt32(TaskVM.User));

        if (item == null)
        {
            return NotFound();
        }

        _taskRepository.NewTask(item);

        var userTask = new UserTask
        {
            Status = "A",
            UserId = users.Id,
            TaskId = item.Id
        };
        _userTaskRepository.NewUserTask(userTask);
        return View("Tasks");
    }

我在提交表单时在var item = new Models.Task行添加了一个断点,而TaskVM对象具有Title,Description和Status字段的所有值,但User值为null。

如果我在浏览器的Inspect选项中捕获请求并从Postman发送它,那就完美了。

在视图或控制器中需要更改什么?

编辑: 这是生成的HTML:

<form class="form-horizontal" name="NewTask" action="/Task" method="post">
<div class="table">
    <dl>
        <dt>
            Title:
        </dt>
        <dd>
            <input class="form-control" name="Title" id="Title" type="text" placeholder="Title" />
        </dd>
        <dt>
            Description:
        </dt>
        <dd>
            <input class="form-control" name="Description" id="Description" type="text" placeholder="Description" />
        </dd>
        <dd>
            <input type="hidden" name="Status" id="Status" value="A" />
        </dd>
        <dt>
            User:
        </dt>
        <dd>
            <select class="form-control" data-val="true" data-val-required="The User field is required." id="User" name="User"><option value="">Please Select One...</option>
                <option value="1">User 1</option>
                <option value="2">User 2</option>
                <option value="3">User 3</option>
                <option value="">etc...</option>
            </select>                                
        </dd>
    </dl>
</div>
<div class="modal-footer">
    <a href="#" class="btn" data-dismiss="modal">Close</a>
    <input type="submit" class="btn btn-primary" id="btnSubmit" />
</div>
<input name="__RequestVerificationToken" type="hidden" value="dQhqzqtPv5jtvF4dmgleh4HqwmIa6w9ancJdYGLWKKWmnnkZS-IjSh9NFSUDeSPsfr9vNHiro1i4a4" />

1 个答案:

答案 0 :(得分:0)

我使用JSon / JQuery将表单发布到我的控制器。

$("#btnSubmit").click(function () {
    submitForm();
});

function submitForm() {
    Task = new Object();
    Task.Title = $("#Title").val();
    Task.Description = $("#Description").val();
    Task.Status = $("#Status").val();
    Task.UserId = $("#UserId").find(":selected").val();
    newTask(Task);
}

function newTask(Task) {
    var s = JSON.parse(JSON.stringify(Task))
    $.ajax({
        url: "http://url:port/Task",
        type: 'POST',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
        },
        datatype: 'json',            
        data: JSON.stringify(s),
        success: function (data) {
            location.reload();
        },
        error: function (request, message, error) {
            location.reload();               
        }
    });
}