Home > database >  Changing the value of a Model.Element serverside is not showing in the View()
Changing the value of a Model.Element serverside is not showing in the View()

Time:11-23

I use a ViewModel to allow user's to enter all kind of Filterdata and then execute them. In certain cases I need to chnge the values at serverside and return the View with those changed data. Sounds simple ;)

Here is where the magic stops: I cannot get the changed data to appear in the View.

This is the View:

@model DrieHamersV4.ViewModels.FilterListVM
@using DrieHamersV4.Helpers;
    
<form asp-action="CreateMailingList" method="get" enctype="multipart/form-data">
    <div>
         <input asp-for="FRoepnaam"  placeholder="@Html.DisplayNameFor(c => Model.FRoepnaam)" />
          <label asp-for="FRoepnaam"></label>                                   
    </div>
    <div>
        <input asp-for="FAchternaam"  placeholder="@Html.DisplayNameFor(c => Model.FAchternaam)" />
        <label asp-for="FAchternaam"></label>
    </div>                                           
    <div>
         <div>@ViewBag.Random</div>                        
    <div>
         <button type="submit">Apply Filter</button>
    </div>
</form>             
     

This is the Controller

    public async Task<IActionResult> CreateMailingList(FilterListVM mailingList)
    {
        // all fields to use as filters
        // 
        // 
        string fFirstName = mailingList.FRoepnaam;
        string fLastName = mailingList.FAchternaam;            
        
        // Testing if the new number `num` will appear in  View()
        Random rnd = new Random();
        int num = rnd.Next(0, 100);
        
        mailingList.FAchternaam = num.ToString(); // When loading works, but not after renewing View()
        ViewBag.Random = (num.ToString()); //Works fine
        
        return View(mailingList);
    }

One way or another the View() refers back to the values when it initially loaded. Changing the values at the clientside works just fine. Changing them at the serverside, however is the problem. The View uses the model @model DrieHamersV4.ViewModels.FilterListVM I am sure I am missing something obvious. Any thoughts?

CodePudding user response:

From your form you're calling CreateMailingList instead of CreateFilterList.

CodePudding user response:

If you want to change a value at serverside of a Model element, you must include a value taghelper in your input-element. An asp-for taghelper does not suffice. I have changed the View accordingly. However, I do not know why it works.

@model DrieHamersV4.ViewModels.FilterListVM
@using DrieHamersV4.Helpers;

<form asp-action="CreateMailingList" method="get" enctype="multipart/form-data">
    <div>
         <input asp-for="FRoepnaam" value="@Model.FRoepnaam"  placeholder="@Html.DisplayNameFor(c => Model.FRoepnaam)" />
          <label asp-for="FRoepnaam"></label>                                   
    </div>
    <div>
        <input asp-for="FAchternaam" value="@Model.FAchternaam"  placeholder="@Html.DisplayNameFor(c => Model.FAchternaam)" />
        <label asp-for="FAchternaam"></label>
    </div>                                       
    
     <div>@ViewBag.Random</div>                        
    <div>
        <button type="submit">Apply Filter</button>
    </div>
  • Related