为什么某些模型属性为空/无?

时间:2019-02-05 14:35:53

标签: asp.net asp.net-mvc vb.net entity-framework model-binding

我正在尝试从视图中发布模型。我发布的模型总是错误的,此刻第二个属性什么都没有。通过不同的方法,有时我会填充展位属性,但是这两个属性中的属性什么都没有。

https://i.imgur.com/KRmpGT4.png

对不起,我英语不好。希望您能理解我的问题。

根据谷歌,模型绑定搞砸了,所以我应该在POST方法中为我的属性使用一个不同的名称。没有工作。我还读到我可以为模型中的每个属性尝试使用两个参数“ Anfang”和“ Ende”。没有工作。

我的模特:

Public Class Stempel
    Public Property ID As Integer
    <Required>
    Public Property Gestempelt As Date
    <Required>
    Public Overridable Property Mitarbeiter As Mitarbeiter
    <Required>
    Public Overridable Property Stempelstati As Stempelstati
End Class

<NotMapped>
Public Class StempelBearbeiten
    Public Property Anfang As Stempel
    Public Property Ende As Stempel
End Class

我的Get-Method,用于编辑数据:

Function StempelBearbeiten(ByVal id As Integer?, ByVal ende As Integer?) As ActionResult
        If id Is Nothing OrElse ende Is Nothing Then
            Return New HttpStatusCodeResult(Net.HttpStatusCode.BadRequest)
        End If
        Dim stempelAnfang = entity.Stempel.Find(id)
        Dim stempelEnde = entity.Stempel.Find(ende)
        If stempelAnfang Is Nothing OrElse stempelEnde Is Nothing Then
            Return New HttpStatusCodeResult(Net.HttpStatusCode.NotFound)
        End If
        Dim t As New StempelBearbeiten With {
            .Anfang = New Stempel With {.ID = stempelAnfang.ID, .Gestempelt = stempelAnfang.Gestempelt, .Stempelstati = stempelAnfang.Stempelstati},
            .Ende = New Stempel With {.ID = stempelEnde.ID, .Gestempelt = stempelEnde.Gestempelt}}
        ViewData("Stempelliste") = New SelectList(entity.Stempelstati.Where(Function(x) x.Aktiv).ToList, "ID", "Bezeichnung", stempelAnfang.Stempelstati.ID)
        Return View(t)
    End Function

我的观点:

@ModelType ZeiterfassungWebMvc.Models.StempelBearbeiten
@Code
ViewData("Title") = "StempelBearbeiten"
End Code
@Using (Html.BeginForm())
@Html.AntiForgeryToken()

@<div Class="container">
    <div Class="text-center">
        <h2> Eintrag bearbeiten</h2>
        <h4>@Model.Anfang.Gestempelt - @Model.Ende.Gestempelt</h4>
    </div>
    <hr />
    <div class="form-horizontal">
        <div class="form-group">
            <div class="row">
                <div class="col">
                    <label>Eingestempelt</label>
                    @Html.HiddenFor(Function(x) x.Anfang.Gestempelt)
                    @Html.DevExpress.DateEditFor(Function(x) x.Anfang.Gestempelt, Sub(settings)
                                                                                      settings.Name = "deEingestempelt"
                                                                                      settings.Theme = "Moderno"
                                                                                      settings.Properties.EditFormat = EditFormat.Custom
                                                                                      settings.Properties.EditFormatString = "dd.MM.yyyy, HH:mm:ss"
                                                                                      settings.Properties.TimeSectionProperties.Visible = True
                                                                                      settings.Properties.TimeSectionProperties.TimeEditProperties.EditFormat = EditFormat.Custom
                                                                                      settings.Properties.TimeSectionProperties.TimeEditProperties.EditFormatString = "HH:mm:ss"
                                                                                  End Sub).GetHtml
                </div>
                <div class="col">
                    <label>Ausgestempelt</label>
                    @Html.HiddenFor(Function(x) x.Ende.Gestempelt)
                    @Html.DevExpress.DateEditFor(Function(x) x.Ende.Gestempelt, Sub(settings)
                                                                                    settings.Name = "deAusgestempelt"
                                                                                    settings.Theme = "Moderno"
                                                                                    settings.Properties.EditFormat = EditFormat.Custom
                                                                                    settings.Properties.EditFormatString = "dd.MM.yyyy, HH:mm:ss"
                                                                                    settings.Properties.TimeSectionProperties.Visible = True
                                                                                    settings.Properties.TimeSectionProperties.TimeEditProperties.EditFormat = EditFormat.Custom
                                                                                    settings.Properties.TimeSectionProperties.TimeEditProperties.EditFormatString = "HH:mm:ss"
                                                                                End Sub).GetHtml
                </div>
            </div>
        </div>
        <div class="form-group">
            <label>Stempelbezeichnung</label>
            @Html.HiddenFor(Function(x) x.Anfang.Stempelstati.ID)
            @Html.DropDownListFor(Function(x) x.Anfang.Stempelstati.ID, DirectCast(ViewData("Stempelliste"), SelectList), New With {.class = "selectpicker form-control", .title = "Bitte wählen Sie eine Rolle aus"})
        </div>

        <div Class="form-group">
            <div Class="btn-group" role="group" aria-label="Bearbeitung">
                <input type="submit" value="Speichern" Class="btn btn-success" />
                @Html.ActionLink("Abbrechen & Zurück", "Übersicht", Nothing, New With {.class = "btn btn-danger"})
            </div>
        </div>
    </div>
</div>
End Using

我的POST方法:

<HttpPost>
    <ValidateAntiForgeryToken>
    <ActionName("StempelBearbeiten")>
    Function Bearbeiten(ByVal brb As Models.StempelBearbeiten) As ActionResult

    End Function

0 个答案:

没有答案