Send multiple parameters to Controller from TextBox on Button Click in ASP.Net MVC

AliYilmaz
 
on Apr 05, 2022 11:00 PM
603 Views

Hi,

I need to update in mvc. I am sending the id but how do I send the other parameters. Is there a way to this? I am sharing my codes. Can you make an example based on my codes?

I tried form collection and failed. I will send multiple parameters.

View

@using EXProject.Models

@{
    ViewBag.Title = "";
}

@foreach (var item in Model)
{
    using (Html.BeginForm())
    {
        <form method="post">
            <div class="row" style="margin-right:0px; margin-left:0px;"> 
                <div class="col-md-6">
                    <!-- HTML5 Inputs -->
                    <div class="card mb-4">
                        <h5 class="card-header">HTML5 Inputs</h5>
                        <div class="card-body">
                            <div class="mb-3 row">
                                <label for="html5-month-input" class="col-md-2 col-form-label">Departman</label>
                                <div class="col-md-10">
                                    <input class="form-control" type="text" value="@item.Department" name="departman" />
                                </div>
                            </div>
                            <div class="mb-3 row">
                                <label for="html5-month-input" class="col-md-2 col-form-label">Hakkında</label>
                                <div class="col-md-10">

                                    <textarea class="form-control" name="hakkinda" value="@item.About" rows="3" multiple id="inputEmail4"></textarea>
                                </div>
                            </div>
                            <div class="mb-3 row">
                                <label for="html5-month-input" class="col-md-2 col-form-label">Başvuru Tarihi</label>
                                <div class="col-md-10">
                                    <input class="form-control" type="text" value="@item.ApplicationDate" name="btarihi" />
                                </div>
                            </div> 
                                <div class="mb-3 row">
                                    <label for="html5-month-input" class="col-md-2 col-form-label">Not</label>
                                    <div class="col-md-10">
                                        <textarea class="form-control" name="note" rows="3" multiple id="inputEmail4"></textarea>
                                       
                                    </div>
                                </div>
                                <div class="mb-3 row">
                                    <label for="html5-month-input" class="col-md-2 col-form-label">Arşiv</label>
                                    <div class="col-md-10">
                                        <input class="form-check-input" type="checkbox" name="check" id="inlineCheckbox1" value="option1">
                                        <label class="form-check-label" for="inlineCheckbox1">Arşive Yolla</label>
                                    </div>
                                </div> 
                            <div class="mb-3 row">
                                <label for="html5-month-input" class="col-md-2 col-form-label">CV</label>
                                <div class="col-md-7">
                                    <input class="form-control" type="text" value="@item.CV" name="cv" />
                                </div>
                                <div class="col-md-3">
                                    <a type="button" href="@Html.ActionLink("Download", "Job", new { file = item.CV })" class="btn rounded-pill btn-dark bx-pull-right">
                                        Cv indir
                                    </a>
                                    <a href="~/Job/Download?file=@item.CV">İndir</a>
                                </div>
                            </div>
                            <br /><br />
                            <div class="mb-3 row">
                                <label for="html5-month-input" class="col-md-2 col-form-label">Güncelle</label>
                                <div class="col-md-10">
                                    <a type="submit" href="@Url.Action("Guncelle", "Job", new { id = item.ID })" class="btn rounded-pill btn-success bx-pull-left">
                                        Güncelle
                                    </a>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    }
}

Controller

public ActionResult Update(int id)
{
    var job = context.JobApplication.Where(x => x.ID == id).FirstOrDefault(); 
    job.Archive = "";
    job.Note = ""; 
    context.SaveChanges();
                
    return View();
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Apr 06, 2022 05:48 AM

Hi ,

Please refer below sample.

Controllers

public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {           
        return View();
    }

    [HttpPost]
    public ActionResult Update(int customerId, string fname)
    {
        int id = customerId;
        string name = fname;
        return View();
    }
}

View

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>    
</head>
<body>
    @using (Html.BeginForm("Update", "Home", FormMethod.Post))
    {
    <table>        
        <tr>
            <th>Customer Id:</th>
            <td><input type="text" id="txtId" name="customerId" /></td>
        </tr>
        <tr>
            <th>Name:</th>
            <td><input type="text" id="txtName" name="fname" /></td>
        </tr>
    </table>
    <input type="submit"  value="Submit" />
    }
</body>
</html>

Screenshot