'Problem sending data from view to controller (MVC)
I want to do simple data saving in mvc. But when I click the button the data is not saved. But I want to pull the data with post method in controller. In the 2 videos I watched on youtube, it does and works like the code I wrote. But my code is not working. Stackoverflow is pushing me to write more text, but I don't know what more to say about this topic. How can I fix?
View:
@using PersonelTakipMVC.Models.Entity;
@model List<Personeller>
@{
ViewBag.Title = "Home Page";
}
@* asp-action="VeriAl" asp-controller="HomeController" *@
<form class="form-group" method="post">
<select name="action:AdiSoyadi">
@foreach (var person in Model)
{
<option>@person.AdiSoyadi</option>
}
</select>
<br /> <br />
<table>
<tr>
<td style="padding-right:20px; text-align:center">
<input name="Giris/Cikis" id="Radio1" type="radio" style="width:50px; height:50px;" /><br />
GİRİŞ
</td>
<td style="text-align: center">
<input name="Giris/Cikis" id="Radio2" type="radio" style="width:50px; height:50px;" /><br />
ÇIKIŞ
</td>
</tr>
</table>
<br />
<br />
<button type="button" class="btn btn-primary">KAYDET</button>
</form>
Controller:
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PersonelTakipMVC.Models.Entity;
namespace PersonelTakipMVC.Controllers
{
public class HomeController : Controller
{
PersonelTakipDBEntities db = new PersonelTakipDBEntities(); //server explorerdaki tablo adı alınır
public ActionResult Index()
{
var personeller = db.Personeller.ToList();
return View(personeller);
}
[HttpPost]
public ActionResult Index(Personeller p1)
{
db.Personeller.Add(p1);
db.SaveChanges();
return Content("data saved");
}
}
}
Solution 1:[1]
Here is a way to do it using Javascript. If I have time I will check if I can do this without Javascript.
SQL:
Model:
namespace SO.Models
{
using System;
using System.Collections.Generic;
public partial class Personnel
{
public int Id { get; set; }
public string AdiSoyadi { get; set; }
public Nullable<bool> GIRIS { get; set; }
public Nullable<bool> CIKIS { get; set; }
}
}
Controller:
public class HomeController : Controller
{
PersonelTakipDBEntities db = new PersonelTakipDBEntities();
public ActionResult Index4()
{
//changed the name of the table for my sake, you can leave your name
var personeller = db.Personnels.ToList();
personeller.Insert(0, new Personnel { AdiSoyadi = "Select an existing AdiSoyadi" });
return View(personeller);
}
[HttpPost]
public ActionResult Index4(Personnel p1) //changed my table name, but you can use yours
{
//put breakpoint here
Personnel toUpdate = db.Personnels.Find(p1.Id);
toUpdate.GIRIS = p1.GIRIS;
toUpdate.CIKIS = p1.CIKIS;
db.SaveChanges();
return Content("data saved");
}
View:
@*using my namespace, but you can use yours*@
@using SO.Models;
@*changed the name of my table, but you can use yours*@
@model List<Personnel>
@{
ViewBag.Title = "Home Page";
}
<head>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
<script>
$(function () {
$("#whatPerson").change(function () {
//var end = this.value;
$("#AdiSoyadi").val($('#whatPerson option:selected').text());
$("#Id").val($('#whatPerson').val());
})
$('#myForm input').on('change', function () {
//this happens on any change
if ($('input[id=GIRIS]:checked', '#myForm').val() == "GIRIS") {
$("#GIRIS").val(true);
$("#CIKIS").val(false);
}
if ($('input[id=CIKIS]:checked', '#myForm').val() == "CIKIS") {
$("#GIRIS").val(false);
$("#CIKIS").val(true);
}
});
});
</script>
</head>
<form class="form-group" method="post" id="myForm">
<select name="action:AdiSoyadi" id="whatPerson">
@foreach (var person in Model)
{
<option value="@person.Id">@person.AdiSoyadi</option>
}
</select>
<br /> <br />
<table>
<tr>
<td><input name="Id" id="Id" type="hidden" /><br /></td>
<td><input name="AdiSoyadi" id="AdiSoyadi" type="hidden" /><br /></td>
<td><input name="GIRIS" id="GIRIS" type="hidden" /></td>
<td><input name="CIKIS" id="CIKIS" type="hidden" /></td>
<td style="padding-right:20px; text-align:center">
<input name="GIRIS" id="GIRIS" type="radio" style="width:50px; height:50px;" value="GIRIS" />GIRKIS<br />
</td>
<td style="text-align: center">
<input name="GIRIS" id="CIKIS" type="radio" style="width:50px; height:50px;" value="CIKIS" />CIKIS<br />
</td>
</tr>
</table>
<br />
<br />
@*change the button to submit*@
<button type="submit" class="btn btn-primary">KAYDET</button>
</form>
Solution 2:[2]
When you want to send data to the controller in you MVC project, you can use javascript to write a request and send your data to controller. If you don't want to write script, then you can only use form submit and I think this is what you want here.
Please see my form below. Firstly, you need to set controller name and action name so that your form can know which controller should accept the request. Then you need to make sure form elements has correct name which is corresponding to the model item name. At last you need to make sure you have a <button> or <input> which type is submit but not button.
@model List<PersonellerModel>
<form class="form-group" method="post" asp-controller="Hello" asp-action="Index">
<select name="AdiSoyadi">
@foreach (var person in Model)
{
<option>@person.AdiSoyadi</option>
}
</select>
<br /> <br />
<table>
<tr>
<td style="padding-right:20px; text-align:center">
<input name="radioBtn" id="Radio1" type="radio" style="width:50px; height:50px;" value="G?R??" /><br />
G?R??
</td>
<td style="text-align: center">
<input name="radioBtn" id="Radio2" type="radio" style="width:50px; height:50px;" value="ÇIKI?"/><br />
ÇIKI?
</td>
</tr>
</table>
<br />
<br />
<button type="submit" class="btn btn-primary">KAYDET</button>
</form>
public class HelloController : Controller
{
public IActionResult Index()
{
var list = new List<PersonellerModel> {
new PersonellerModel{ AdiSoyadi = "option1"},
new PersonellerModel{ AdiSoyadi = "option2"},
new PersonellerModel{ AdiSoyadi = "option3"}
};
return View(list);
}
[HttpPost]
public ActionResult Index(PersonellerModel p1)
{
var a = p1.AdiSoyadi;
return Content("data saved");
}
}
public class PersonellerModel
{
public string AdiSoyadi { get; set; }
public string radioBtn { get; set; }
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Tiny Wang |


