微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

模型从具有局部视图的表单返回给控制器的null

如何解决模型从具有局部视图的表单返回给控制器的null

我有一个表单,其中部分内容来自局部视图,但是当我将表单发布回控制器时,从控制器填充的部分为空

查看:

@using (Html.BeginForm(null,null,FormMethod.Post,new { @class = "form-horizontal" }))
{
    <div class="card">
        <div class="card-header bg-institution">
            <div class="card-title">@Global.createInstitution</div>
        </div>
        <div class="card-body">

            @Html.ValidationSummary(true)

            <div class="card-title text-muted">@Global.institution</div>

            @Html.FormElement(model => model.Institution.NAVN,InputType.Text,new { autofocus = "" })
            @Html.FormElement(model => model.Institution.FSE_NAVN)

            <div class="form-group">
                @Html.Formlabel(model => model.Institution.FINANSIERINGSMODEL)
                <div class="col-sm-8">
                    @Html.DropDownListFor(model => model.Institution.FINANSIERINGSMODEL,ViewData["finansieringsmodeller"] as SelectList,"- " + Global.choose + " -",new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Institution.FINANSIERINGSMODEL)
                </div>
            </div>

            <div class="form-group">
                @Html.Formlabel(model => model.Institution.INSTITUTIONSTYPE)
                <div class="col-sm-8">
                    @Html.DropDownListFor(model => model.Institution.INSTITUTIONSTYPE,new SelectList(string.Empty,"Value","Text"),new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Institution.INSTITUTIONSTYPE)
                </div>
            </div>

            <div class="form-group">
                @Html.Formlabel(model => model.Institution.LMDB_KLASSIFIKATION)
                <div class="col-sm-8">
                    @Html.DropDownListFor(model => model.Institution.LMDB_KLASSIFIKATION,ViewBag.LdbKlassifikationer as SelectList,new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Institution.LMDB_KLASSIFIKATION)
                </div>
            </div>

            <div class="form-group">
                @Html.Formlabel(model => model.Institution.FSE_TEAM)
                <div class="col-sm-8">
                    @Html.DropDownListFor(model => model.Institution.FSE_TEAM,ViewBag.fseteam as SelectList,new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.Institution.FSE_TEAM)
                </div>
            </div>

            @Html.FormElement(model => model.Institution.MAX_ADMIN)
            @Html.FormElement(model => model.Institution.KOMMENTAR)

            <div class="form-group">
                <div class="col-sm-8">
                    <table>
                        <tr>
                            <td>
                                @Html.LabelFor(model => model.Institution.KONTAKTCLaim)
                            </td>
                            <td>&nbsp;&nbsp;</td>
                            <td>
                                @Html.CheckBoxFor(model => model.Institution.HarKontaktClaim,new { @class = "largerCheckBox checkBox" })
                            </td>
                        </tr>
                    </table>
                </div>
            </div>

            <hr />

            <div id="ansv-forsker-section">
                <div class="card-title text-muted">@Global.authRespUser</div>
                    @{
                        Html.RenderPartial("_CreateForsker",Model.NewForsker);
                    }
                </div>

                <hr />

                <div class="form-group">
                    <div class="col-sm-8">
                        <br />
                        <table>
                            <tr>
                                <td>
                                    @Html.LabelFor(model => model.MaaHjemsendeBool,"control-label",true)
                                </td>
                                <td>&nbsp;&nbsp;</td>
                                <td>
                                    @Html.CheckBoxFor(model => model.MaaHjemsendeBool,new { @class = "largerCheckBox checkBox",@Checked = "checked" })
                                </td>
                            </tr>
                        </table>
                    </div>
                </div>

                <hr />

                <div class="form-group">
                    <div class="col-md-offset-2">
                        <input type="submit" value=@Global.create class="btn btn-primary" />
                        <a href="@Session["UrlAnnuller"]" class="btn btn-primary">
                            <span>@Global.cancel</span>
                        </a>
                    </div>
                </div>

                @Html.HiddenFor(model => model.IsExistingForsker)
                @Html.HiddenFor(model => model.ExistingForskerIdent)
            </div>
    </div>
}  

局部视图:

````@using System.Security.Claims
@model Dst.FSEOnline.Data.Entities.FORSKER



 @{
bool HasForskerClaim = ((ClaimsIdentity)ViewContext.HttpContext.User.Identity).HasClaim(ClaimTypes.Role,"Forsker") ? true : false;

var forskerClaimRouteval = new { httproute = "",action = "GetForskerAutocompleteList",controller = "ForskerApi",instNr = ViewBag.InstNrAsstring };
var acForskerClaimUrl = Url.RouteUrl("DefaultApi",forskerClaimRouteval);

var forskerRouteval = new { httproute = "",action = "GetAutocompleteList",controller = "ForskerApi" };
var acForskerUrl = Url.RouteUrl("DefaultApi",forskerRouteval);
}
@if (HasForskerClaim)
 {
    @Html.FormElement(model => model.NAVN,new { data_fseAutocompleteFa = acForskerClaimUrl })
 }
else
 {
    @Html.FormElement(model => model.NAVN,new { data_fseAutocompleteFa = acForskerUrl })

    @Html.FormElement(model => model.MAIL)
    @Html.FormElement(model => model.KONTAKT_TELEFON,htmlAttributes: new { @class = "remove-spaces" })
    @Html.FormElement(model => model.KOMMENTAR)

    <div class="form-group">
        <div class="col-sm-8">
            <br />
            <table>
                <tr>
                    <td>
                        @Html.LabelFor(model => model.InEnglish)
                    </td>
                    <td>&nbsp;&nbsp;</td>
                    <td>
                        @Html.CheckBoxFor(model => model.InEnglish,new { @class = "largerCheckBox checkBox" })
                    </td>
                </tr>
            </table>
        </div>
    </div>

    <div class="form-group">
        @Html.Formlabel(model => model.TOKEN_TYPE)
        <div class="col-sm-8">
            @Html.DropDownListFor(model => model.TOKEN_TYPE,ViewBag.TokenTypeList as SelectList,"- " + Resources.Global.choose + " -",new { @class = "form-control",@id = "ValgtTokenType" })
            @Html.ValidationMessageFor(model => model.TOKEN_TYPE)
        </div>
    </div>
    <div id="SmsToken">
        @Html.FormElement(model => model.SMS_TOKEN_MOBIL,htmlAttributes: new { @class = "remove-spaces" },SAEtStjerne: true)
    </div>
    <div id="FysiskToken">
        @Html.FormElement(model => model.TOKEN_ADR,htmlAttributes: new { @placeholder = "Adresse til udsendelse af token" },SAEtStjerne: true)
    </div>
}

@ Resources.Global.resetUser @ Resources.Global.errorExistingUser @ Resources.Global.choose @ Resources.Global.cancel
<script>
    $(function() {

        var initToken = function() {
            var token = @Html.Raw(Json.Encode(Model.TOKEN_TYPE));

            if (token === 1) {
                        $("#SmsToken").hide();
                        $("#FysiskToken").show();
                    }
                    else if (token === 2) {
                        $("#SmsToken").show();
                        $("#FysiskToken").hide();
                    }
                    else {
                    $("#SmsToken").hide();
                    $("#FysiskToken").hide();
                    }

        };

        $(initToken());

        $("#ValgtTokenType").change(function() {
            //console.log("State-changed " + this.value);
            if (this.value == 1) {
                $("#SmsToken").hide();
                $("#FysiskToken").show();
            }
            else if (this.value == 2) {
                $("#SmsToken").show();
                $("#FysiskToken").hide();
            }
            else {
                $("#SmsToken").hide();
                $("#FysiskToken").hide();
            }
        });

        var valgtForsker = {
            divNewForsker: $("#NewForsker"),divexistingForsker: $("#ExistingForsker"),navn: $("#NAVN"),IsExistingForsker: $("#IsExistingForsker"),forskerIdent: $("#FORSKER_IDENT"),model: {},resetButton: $("#btResetForskerValg"),existingForskerIdent: $("#ExistingForskerIdent"),bekraeft: function() {
                this.IsExistingForsker.val(true).change();
                this.existingForskerIdent.val(this.model.value);
                var url = "@Url.Action("IndexPartial","Forsker")";
                $(".forskerdata",this.divexistingForsker).load(url,{ id: valgtForsker.model.value });
            },reset: function() {
                this.existingForskerIdent.val("");
                this.IsExistingForsker.val(false).change();
            }
        };


        $("#IsExistingForsker").change(function() {
            //console.log("State-changed " + this.value);
            var isExistingForsker = this.value == 'true';
            if (isExistingForsker) {
                valgtForsker.divNewForsker.hide();
                valgtForsker.divexistingForsker.show();
            } else {
                $("#NAVN").val("");
                valgtForsker.divexistingForsker.hide();
                valgtForsker.divNewForsker.show();
            }
        });


        $("#btBekraeftForsker").click(function() {
            //console.log(valgtForsker);
            valgtForsker.bekraeft();
        });

        valgtForsker.resetButton.click(function() {
            valgtForsker.reset();
        });


        var createAutocomplete = function() {
            var obj1 = {
                elem: null,options: {
                    autoFocus: true,delay: 400,minLength: 2,select: function(event,ui) {
                        valgtForsker.model = ui.item;
                        var div = $("#ForskerDetailFull");
                        var url = "@Url.Action("IndexPartial","Forsker")";
                        div.load(url,{ id: valgtForsker.model.value });

                        $("#dialogBekraeftForsker").modal('show').on('shown.bs.modal',function (event) {
                        $("#btBekraeftForsker").focus();
                        });
                        return false;
                    }
                },startAutcomplete: function(data) {
                    this.options.source = data;
                    $(this.elem).autocomplete(this.options);
                },init: function(elem) {
                    this.elem = elem;
                    var url = $(this.elem).attr("data-fseAutocompleteFa");
                    var proxy = $.proxy(this.startAutcomplete,this);
                    $.get(url,proxy);
                }
            };

            obj1.init(this);

        };


        /* Start */
        $("Input[data-fseAutocompleteFa]").each(createAutocomplete);

        if (valgtForsker.existingForskerIdent.val().length > 0) {
            valgtForsker.model.value = valgtForsker.existingForskerIdent.val();
            valgtForsker.bekraeft();
        }

        $("#IsExistingForsker").change();

    });

````

控制器:

    [HttpPost]
    [ClaimsAuthorize(ClaimType = "FSE-Godkender")]
    public ActionResult Create(OpretInstitution model)
    {
        try
        {....

  

其模型。NewForsker,其中所有字段始终为空

最后是模型:

public class OpretInstitution 
{
    public INSTITUTION Institution { get; set; }
    public FORSKER NewForsker { get; set; }
    
    public string ExistingForskerIdent { get; set; } 
    public bool IsExistingForsker { get; set; }
    public bool MaaHjemsendeBool { get; set; }
    
    public OpretInstitution()
    {
        Institution = new INSTITUTION();
        NewForsker = new FORSKER();
        IsExistingForsker = false;
        MaaHjemsendeBool = true;
    }
 }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。