2
Answers

how bind two action methods in same view page using Asp.net MVC

Photo of anand m

anand m

May 30
266
1

Hello Team,

Currently I am working in development project  and I am trying to load in page load dropdown and texbox in the same .Cshtml page.

But  to call two action methods in the same view.cshml , I have texbox other details I am using one action method and other dropdown I am using another action method calling to laod from DB.

1. public IActionResult Index(H1BOptionDetails H1Bdetails)(textbox data loading using this action method)
2.public async Task<IActionResult> Details()- loading data to dropdown using this action methods.

<form action method I am calling only one action method and how to call two action methods in the same view.csthl page?
 

Answers (2)

1
Photo of Amit Mohanty
16 52.6k 6.1m Jun 03

With a .cshtml Razor view, you cannot directly call two action methods from a single <form> submission. However, there are clean ways to achieve this, load data for a dropdown and a textbox on the same page, even though they come from different actions.

Merge both data loading steps inside the Index action method, so that everything is loaded in one request, and all required data is passed to the view.

Step 1: Create a ViewModel

public class MyPageViewModel
{
    public H1BOptionDetails H1BDetails { get; set; }
    public List<SelectListItem> DropdownOptions { get; set; }
}

Step 2: Update Controller Actions

public async Task<IActionResult> Index()
{
    var model = new MyPageViewModel();

    // 1. Load dropdown options (originally from Details())
    var dropdownData = await _yourService.GetDropdownDataAsync();
    model.DropdownOptions = dropdownData
        .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() })
        .ToList();

    // 2. Optionally load default H1B details (textbox)
    model.H1BDetails = new H1BOptionDetails(); // or prepopulate from DB

    return View(model);
}

[HttpPost]
public async Task<IActionResult> Index(MyPageViewModel model)
{
    if (ModelState.IsValid)
    {
        // Save H1BDetails (from textbox)
        await _yourService.SaveH1BDetails(model.H1BDetails);
        ViewBag.Message = "Saved successfully!";
    }

    // Reload dropdown in case the page needs to be returned
    var dropdownData = await _yourService.GetDropdownDataAsync();
    model.DropdownOptions = dropdownData
        .Select(x => new SelectListItem { Text = x.Name, Value = x.Id.ToString() })
        .ToList();

    return View(model);
}

Step 3: Update View (Index.cshtml)

@model MyPageViewModel

<form asp-action="Index" method="post">
    <!-- Dropdown -->
    <label for="SomeDropdown">Select Option:</label>
    <select asp-for="H1BDetails.OptionId" asp-items="Model.DropdownOptions"></select>

    <!-- Textbox -->
    <label for="Detail">Enter Detail:</label>
    <input asp-for="H1BDetails.Detail" />

    <button type="submit">Submit</button>
</form>

Alternatively you can use AJAX for Dropdown also. If dropdown data is dynamic or needs to be refreshed independently, you can use AJAX to load it from Details() into a partial view or directly populate it with JavaScript, but for most cases, the controller composed model approach is cleaner and easier to manage.

0
Photo of Muhammad Imran Ansari
214 9k 345k Jun 03

In ASP.NET Core MVC, you cannot directly call two action methods from a single <form> action. However, you can load data for both the textbox and dropdown in a single action method (typically the Index action) and bind them in the view.

The recommended approach is to use a ViewModel and combine both data loads in one action:

// Create a ViewModel

public class H1BViewModel
{
    public H1BOptionDetails H1BDetails { get; set; }       // For textbox
    public List<SelectListItem> DropdownOptions { get; set; } // For dropdown
}
// Update the Index Action Method

public async Task<IActionResult> Index()
{
    // Load textbox data
    var h1bDetails = await GetH1BDetailsAsync(); // Your DB call for textbox

    // Load dropdown data
    var dropdownData = await GetDropdownDataAsync(); // Your DB call for dropdown
    var dropdownOptions = dropdownData.Select(x => new SelectListItem
    {
        Value = x.Id.ToString(),
        Text = x.Name
    }).ToList();

    // Build the ViewModel
    var viewModel = new H1BViewModel
    {
        H1BDetails = h1bDetails,
        DropdownOptions = dropdownOptions
    };

    return View(viewModel);
}
// Update Your View (.cshtml)

@model YourNamespace.H1BViewModel

<form asp-action="Index" method="post">
    <!-- Textbox -->
    <input type="text" asp-for="H1BDetails.SomeProperty" class="form-control" />

    <!-- Dropdown -->
    <select asp-for="H1BDetails.SelectedOptionId" asp-items="Model.DropdownOptions" class="form-control"></select>

    <button type="submit">Submit</button>
</form>

Good Luck!