1

First, I'd like to point out that I'm using .NET Core with MVC.

I have a method (GetMember) of the async Task <IActionResult> type of my Members controller that receives an id (the id of the user) and sends it to an external database (via Filemaker API).
In return, I receive an IEnumerable that contains my result. (Until then everything is fine, it works perfectly).

My problem is that I can not put this data in my view.
Indeed, I have a form and I would like the data received to be automatically entered in my form.

My problem results in passing my variable results to my model, I can not find how to send data from one to another.

I know however to use the data of my existing model Members, but if you want I can add the code of the view.

I deliberately deleted the piece of code that is supposed to perform this operation because the result was wrong.

Could you give me a hand?

EDIT/ with code refreshed, i now receive an error 500 on Chrome when i open the page... any idea?

Controller

[HttpGet]
    public async Task<IActionResult> GetMember(int? id)
    {
        if(id == null)
        {
            return NotFound();
        }

        try
        {
        FileMakerRestClient client = new FileMakerRestClient("https://fms171.hostmy.solutions", "helloJAK", "jak", "legeneral!");
        var toFind = new Models.Members { Zkp_WEB = id };
        var results = await client.FindAsync(toFind);

            bool isEmpty = !results.Any();
            if (isEmpty)
            {
                return NotFound();
            }

        return View(results);
        }
        catch
        {
            return BadRequest();
        }
    }

View

<script type="text/javascript">

    $(document).ready(function () {
        GetMember();

    }); 

        function GetMember() {
            $.ajax({
                url: "https://localhost:44338/Members/GetMember/" + 28
            });
    }

</script>
Korpin
  • 323
  • 6
  • 24
  • I don't quite understand. This is a REST controller, and you want to re-use the same one with a HTML view as well? Or are you calling this controller from your view with javascript, and want to work with the result? – gnud Nov 21 '18 at 20:58
  • I'm calling this controller from my view with javascript and i do want to work with the result. Its to load the form with the info of a "member" when he enter in the webpage. – Korpin Nov 21 '18 at 21:01
  • Oh, now I think I understand. Your `result` is some different type, it's not your model type, and you're wondering how to turn this Filemaker type into objects of your `Members` class? – gnud Nov 21 '18 at 21:08
  • Well, results is a IEnumerable and i want my view to be form to be loaded with it :p – Korpin Nov 21 '18 at 21:20
  • OK - then the code looks correct at first glance. Have you used the developer tools in your browser to look at what gets sent back and forth? If you want the result as JSON, you probably have to specify JSON in the `Accept` header in your request. – gnud Nov 21 '18 at 21:22
  • I think you dont need to use JsonResult with .NET Core anymore. I think im close, just a problem in a view now.. guess it is because on my script... – Korpin Nov 21 '18 at 22:08
  • 500 is an application error on the server. You should be able to set breakpoints in your code and debug and see exactly what error your code is throwing and on what line. That would help to troubleshoot – Jonathan Nov 21 '18 at 23:13
  • @Jonathan, Yup, i know what's wrong actually :p its a "data" problem in my ajax code :p im trying to fix it :p – Korpin Nov 21 '18 at 23:22

1 Answers1

1

I don't quite understand what are you trying. But I guess you have some problem getting form data from the view. You need two action method for this; one for GET method and another one for POST method. In controller:

public async Task<IActionResult> Method()
{ 
   return View();
}

[HttpPost]
public async Task<IActionResult> Method(Model model)
{ 
   if (ModelState.IsValid)
   {
     // work with the model
   } else return View(model)
}

In View:

@model Model

<form asp-action="ActionName" method="post"> 
   <div> 
     <label asp-for = "Name"></label> 
     <input asp-for = "Name" /> 
     <span asp-validation-for = "Name"></span> 
   </div> 
   <div> 
     <input type = "submit" value = "Save" /> 
   </div> 
 </form>

You should look at this tutorial. https://learn.microsoft.com/en-us/aspnet/core/tutorials/first-mvc-app/?view=aspnetcore-2.1

mafirat
  • 258
  • 1
  • 8
  • Thanks for your help! This tutorial helped me a lot! Now, when i receive a error 500 when i execute my webpage, any idea? I just edited my code to let you see – Korpin Nov 21 '18 at 22:02
  • Okey, i figured out why it does not work! But can't make it work! I've a problem in my ajax code.. – Korpin Nov 21 '18 at 23:51
  • 1
    You need to add type attribute your Ajax request. Check this topic, https://stackoverflow.com/questions/9436534/ajax-tutorial-for-post-and-get, but I think, you don't need Ajax in this case. If you want to show current user information in a view, Just keep your member ID in session and in action, get user information from session, get data with this id and pass results to the view. – mafirat Nov 22 '18 at 07:52