ASP.net Core ViewComponent validate model on http post RRS feed

  • Question

  • I created a view component that contains a form with user inputs fields like textbox, dropdown ...etc. I tried to leave a required input field empty when I submitted the form. I got the correct result with error on required field validation and the textbox field did not render the old value it just return empty with required massage. 

    also I tried to validate expression on comparing two fields and I got a correct message that two input fields are not the same.

    the question is: 

    How on post and submitting the form the model did not repopulate the old values. even in the component action I am repopulating the data.

    does view component preserve user input if model is not valid?

       public IViewComponentResult Invoke(NavigationDestination navigationDestination)
                var model = new IncomeViewModel();

                 // Code to Build the model

                return View(model);

    Bassam wassouf

    • Moved by CoolDadTx Monday, July 23, 2018 6:08 PM ASP.NET related
    Monday, July 23, 2018 2:34 PM

All replies

  • does view component preserve user input if model is not valid?

    It's the model or what should be used is a view model that holds all data that persist data to the view. 



    Monday, July 23, 2018 4:27 PM
  • I have a view model as you see in the Invoke action and I am building the model. But on post (Submit) if the modelstate is invalid I am not getting the persisted data  I am getting back the updated user inputs.

    if user change date to a new date which is invalid selected date. on post the date would not be reloaded from the persisted data (database) it will have a invalid user input. 

    The question now how that happened? Is ViewAction technique try to cash the data on invalid model state   

    Bassam wassouf

    Monday, July 23, 2018 5:51 PM
  • Please post questions related to ASP.NET in the ASP.NET forums.

    Michael Taylor http://www.michaeltaylorp3.net

    Monday, July 23, 2018 6:08 PM