如何将模型从视图发送到控制器

时间:2019-07-11 14:08:57

标签: c# asp.net-mvc-5

当我尝试将模型发送到控制器时,我得到一个null,但是如果我尝试获取单输入,而不是整个模型,则它起作用。我具有表单中的所有Arias类字段,然后将其发送给控制器时,控制器接收到它为null无关紧要,无论我是否填写输入。

第一个是Areas类。 第二个是控制器。 然后是视图。

public class Areas
{
    [Key]
    [Required(ErrorMessageResourceType = typeof(Webinfo), ErrorMessageResourceName = "msg_campo_requerido")]
    [Display(Name = "field_id", ResourceType = typeof(Fields))]
    public int area_id { get; set; }

    [Required(ErrorMessageResourceType = typeof(Webinfo), ErrorMessageResourceName = "msg_campo_requerido")]
    [Display(Name = "field_area", ResourceType = typeof(Fields))]
    [StringLength(25, ErrorMessageResourceType = typeof(Webinfo), ErrorMessageResourceName = "msg_longitud")]
    public string area { get; set; }
}

public async Task<ActionResult> Create(AndreinaDAL.Areas.Maestro.Areas area)
{
    if (!Auth.validar_acceso_app(80))
    {
        return RedirectToAction("Index", "Home", new { area = "" });
    }
    if (ModelState.IsValid)
    {
        ResponseModel response = await apiService.Post<AndreinaDAL.Areas.Maestro.Areas>("Areas", "Create", area);

        if (response.IsSuccess)
        {
            RightMessage = response.Message;
            return RedirectToAction("Index");
        }
        else
        {
            ViewBag.Error = response.Message;
        }
    }

    return View(area);
}
@model AndreinaDAL.Areas.Maestro.Areas
@using AndreinaWeb.Authentication
@using AndreinaCORE.Resource
@{
    ViewBag.Title = "Create";
}

<h2>Create</h2>
    <div class="row">
        <div class="col-md-12">
            @using (Html.BeginForm("Create", "Areas", FormMethod.Post, new { @class = "form-horizontal" }))
            {
                @Html.AntiForgeryToken()
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="panel panel-default">
                    <div class="panel-heading">
                        <h1 class="panel-title">
                            <span class="fa fa-arrow-circle-o-left"></span>
                            <strong>@Webinfo.msg_application_create</strong>
                        </h1>
                    </div>
                    <div class="panel-body">
                        @if (@ViewBag.Error != null)
                        {
                            <div class="alert alert-danger" id="infoAlert" role="alert">
                                <strong>@ViewBag.Error!</strong>
                            </div>
                        }
                        <div class="alert alert-info" role="alert">
                            <button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                            <strong>@Webinfo.msg_info </strong> @Webinfo.msg_create
                        </div>
                    </div>
                    <div class="panel-body">
                        @Html.HiddenFor(m => m.area_id)
                        <div class="form-group">
                            <label class="col-md-3 col-xs-12 control-label">@Fields.field_area</label>
                            <div class="col-md-6 col-xs-12">
                                <div class="input-group">
                                <span class="input-group-addon"></span>
                                    @Html.TextBoxFor(m => m.area , "", new { @class = "form-control" })
                            </div>
                            <span class="help-block">@Fields.field_area</span>
                            <span class="help-block">@Html.ValidationMessageFor(m => m.area, "", new { @class = "text-danger" })</span>
                        </div>
                    </div>
                </div>
                <div class="panel-footer">
                    <a class="btn btn-primary pull-left" href="/Maestro/Areas">@Buttons.btn_pagina_anterior</a>
                    <button class="btn btn-primary pull-right" onclick="loading()">@Buttons.btn_guardar</button>
                </div>
            </div>
        }
    </div>
</div>

0 个答案:

没有答案