Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
166 views
in Technique[技术] by (71.8m points)

c# - How to display model errors form web api in xamarin forms

I have a web api, mvc website and xamarin forms app (android and ios). I also have a class library to communicate with the api in my solution. I have successfully got all of them working however I'm stuck with model errors in the xamarin forms app. In the mvc website, I have the below code to decode model errors, I pass the response to it when it fails.

protected void AddResponseErrorsToModelState(ApiResponse response)
    {
        System.Collections.Generic.IDictionary<string, string[]> errors = response.ErrorState.ModelState;
        if (errors == null)
        {
            TempData["ERR"] = "An unexpected error occured.";
            return;
        }

        foreach (System.Collections.Generic.KeyValuePair<string, string[]> error in errors)
        {
            // add model error for empty keys
            if (string.IsNullOrEmpty(error.Key))
            {
                ModelState.AddModelError("", error.Value[0]);
            }

            // add model properties errors
            foreach (System.Collections.Generic.KeyValuePair<string, ModelState> entry in
                from entry in ModelState
                let matchSuffix = string.Concat(".", entry.Key)
                where error.Key.EndsWith(matchSuffix)
                select entry)
            {
                ModelState.AddModelError(entry.Key, error.Value[0]);
            }
        }
    }

Then the error simply get display in the view with the below line.

 @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" })

The issue now is I don't have an idea how to display these model errors in xamarin forms app. I have been able to bind the view model to the page:

BindingContext = new RegisterModel();

And I submit the model on submit with this:

public Command RegisterCommand => new Command(async () =>
    {
        var response = await loginClient.Register(this);

        if (response.StatusIsSuccessful)
        {
            // go login page
            string message = "Registration successful, login to continue.";
            await Shell.Current.GoToAsync($"login?message={message}");
        }
        else
        {
            await Application.Current.MainPage.DisplayAlert("Error", "An error occured", "Try Again");
        }
    });

When there's error, how do I display the errors in the page as I was doing in the mvc website, please help me.

question from:https://stackoverflow.com/questions/66046906/how-to-display-model-errors-form-web-api-in-xamarin-forms

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...