1

The use case is this: the user can enter a client number and I can then do an API call and get all their information and repopulate the form.

The form is say at /Users/Add.

I don't want just reload the form with this data because it will wipe out all their other data (assume for this example they are entering a fruit order, so they can specific their name, email address, fruit type, fruit number).

So the question is how can I call the controller and repopulate the model without wiping out user entered data in the form?

Assume that they entered the fruit type and fruit number, and entered their client Id and clicked a button. The button click calls the controller to get the client name/client email address. I could just load the view with this data but it will overwrite the fruit type and fruit number fields.

2
  • 1
    It sounds like the technology you're looking for is AJAX. If you're just unfamiliar with the term then you'll find lots of tutorials and examples online. It's essentially using JavaScript code to send a request to a URL at the server and receive the response, then that code can do whatever you like with the response in the browser. (e.g. Update elements on the page.) Commented Jun 27 at 13:30
  • 1
    In addition to David, you can also use plain Javascript with the Fetch-command; Commented Jun 27 at 14:34

2 Answers 2

2

You can implement a Javascript function that will send a request, like

function sendRequest(type, url, callback, async, params) {
    if (async !== false) async = true;
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = callback;
    xhttp.open(type, url, async);
    xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xhttp.send(params);
}

type is the request type. It could be GET or POST, etc.

url is the path to the end point you want to send request to.

callback is a function to be executed when the request is responded to

async determines whether the request is to be asynchronous. If true, then Javascript will not wait for the response, but proceed with its work instead and when the response is received, then the callback will be executed.

params are the parameters to be passed.

The inner block of the function is an AJAX request to be sent. You can see it documented all over the place on the internet.

Now, you mentioned a form and a button. If this button happens to be a submit, which submits the form, then you can add a submit event, like

yourForm.addEventListener(
    "submit",
    function(event)
    {
        event.preventDefault();
        sendRequest("POST", yoururl, function() {
            //do something
        }, true, yourparams);
    },
    false
);

In the above you will need to find your form element, that is what yourForm represents, provide the URL, that's what yoururl represents and pass yourparams, whatever they may be.

Sign up to request clarification or add additional context in comments.

Comments

1

I am not sure if this answers your question, but as I understand it:

  • you have a model bound to your page, which has multiple inputs and possible return fields e.g.:

What you could do is:

assuming you have 2 models: User and FruitOrder

Create a ViewModel:

public class UserAndFruitVm
{
    public User Userdata {get; set; }
    public FruitOrder {get; set;}
}

In your controller change the Index method

namespace YourProject.Controllers
{
    public class FruitController(ApplicationDbContext context) : Controller
    { 
        private readonly ApplicationDbContext _context = context;

         public IActionResult Index(UserAndFruitVm modelData)
         { 
            if(modelData.UserData.Id != null)
            {
                modelData.UserData = _context.User.Find(id);  // gets all the userdata
            }
          }
          return View(modelData);
      }
 }  

And in your Index View change accordingly

@model YourProject.ViewModels.UserAndFruitVm

<form id="frmIndex" asp-action="Index" asp-controller="Fruit" enctype="multipart/form-data" method="post">

<div>
  <label> type user id </label>
  <input asp-for="User.Id"/>
</div>
<div>@html.displayfor(model => Model.User.Name)</div>
<div>@html.displayfor(model => Model.User.Description)</div>
<div>@html.displayfor(...)</div>
<div><input type=submit value="Get User"/>


<input asp-for="FruitOrder.Id" />
<input asp-for="FruitOrder.Quantity"
<input asp-for="FruitOrder.Type"
<input asp-for=(..other fruitorderfields.../)

</form>

<div>
<button type="submit" form="formIndex" asp-action="Create" asp-controller="Fruit">Save Order</button>
</div>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.