POST przekazuje różne wartości

0

Witam,
Jestem początkujący w ASP.NET MVC i podczas pisania testowego programu napotkałem błąd , z którym niw mogę sobie poradzić.
Oto szczegóły.
W katalogu aplikacji Models mam enum TestTyp i klasę Test:

namespace MB.Models
{
    public enum TestTyp
    {
        enumTest1,
        enumTest2,
        enumTest3
    }

    public class Test
    {
        public int Id { get; set; }
        public string stkTest1 { get; set; }
        public string stkTest2 { get; set; }
        public TestTyp Typ { get; set; }
    }
}

W katalogu aplikacji ViewModels mam klasę TestEditViewModel:

using MB.Models;
namespace MB.ViewModels
{
    public class TestEditViewModel
    {
        public string stkTest1 { get; set; }
        public string stkTest2 { get; set; }
        public TestTyp Typ { get; set; }
    }
}

Teraz po wyświtleniu w przeglądarce z Views/Test/Create.cshtml

@using MB.Models
@model Test

<div>
    <h1>Create</h1>
    <div>
        @using (Html.BeginForm())
        {
            <div>
                @Html.LabelFor(r => r.stkTest1 )
                @Html.EditorFor(r => r.stkTest1 )
            </div>
           
            <div>
                @Html.LabelFor(r => r.Typ)
                @Html.DropDownListFor(r => r.Typ, Html.GetEnumSelectList(typeof(TestTyp)), "wybierz")
            </div>

            <div>
                <input type="submit" value="Zapisz" />
            </div>
        }
    </div>
</div>

i wprowadzeniu danych podczas zapisu mam ustawiony breakpoint w kontrlolerze gdzie mam:

...

 [HttpGet]
        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Create(TestEditViewModel model)
        {      **// tu mam ustawiony breakpoint**
                var newTest = new Test();
                newTest.stkTest1 = model.stkTest1;
                newTest.stkTest2 = model.stkTest2;
                newTest.Typ = model.Typ;

               .....
        }

....

model zawiera:

        stkTest1 = "Wpisany_string"
        stkTest2 = null
        Typ = "enumTest2" //wybrany z listy

czyli wszystko OK!!

ale jeżeli do Create.cshtml dodam obsługę stkTest2 czyli mam:

           <div>
                @Html.LabelFor(r => r.stkTest1 )
                @Html.EditorFor(r => r.stkTest1 )
            </div>
             <div>
                @Html.LabelFor(r => r.stkTest2 )
                @Html.EditorFor(r => r.stkTest2 )
            </div>
           
            <div>
                @Html.LabelFor(r => r.Typ)
                @Html.DropDownListFor(r => r.Typ, Html.GetEnumSelectList(typeof(TestTyp)), "wybierz")
            </div>

            <div>
                <input type="submit" value="Zapisz" />
            </div>

wtedy model zawiera:

        stkTest1 = null
        stkTest2 = null
        Typ = "enumTest1"

pomimo wprowadzenia innych wartości.

I właśnie tego nie mogę przejść. Dlaczego tak się dzieje?
Za pomoc z góry dziękuję.
Pozdrawiam

1

A przy editowaniu to ty jakiś model przekazujesz do view?

0

Witam,
w zamieszczonym na forum kodzie pola klas zmieniłem chcąc uprościć jak najbardziej moje pytanie.
Ale w rzeczywistym projekcie jedno z pól w obu klasach posiadało nazwę Model czyli:
public string Model {get; set;}
i to było przyczyną błędu!
Po zmianie nazwy na MojModel wszystko działa prawidłowo.
Pozdrawiam

1 użytkowników online, w tym zalogowanych: 0, gości: 1