0

Scenario

I am going to create a simple interface to insert donations from students using Asp.net Core MVC and EF. According to the App, before entering donation I have to display student information (from Student table) by using student's Admission number (Add_No). Then I have to insert donation data using the same view but using a different submit button to the table Welfare.

Problem:

I tried it as following ways as shown in my code blocks. Insertion is successful. But I couldn't show student info on the screen even though I retrieve them.

Code for View.cshtml

@model Student_Management.Models.Welfare

@{
    ViewData["Title"] = "Wcreate";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h4 align="center">Welfare Donation Entry Form</h4>

<hr />
<table>
    <tr>
        <td>
            @using (Html.BeginForm("getStudentInfo", "Payments", FormMethod.Post))
            {
                <div class="form-group">
                    <label asp-for="Add_No" class="control-label"></label>
                    <input asp-for="Add_No" class="form-control" id="t1" name="A" />
                    <span asp-validation-for="Add_No" class="text-danger"></span>
                </div>

                <div class="form-group">
                    <input type="submit" value="getStudentInfo" class="btn btn-primary" id="btngetStudentInfo" />
                </div>
            }
        </td>
        <td>
            <ul>
                @if (ViewBag.students != null)
                {
                    @foreach (var std in ViewBag.students)
                    {
                        <li>
                            @std
                        </li>
                    }
                }
            </ul>
        </td>
    </tr>
</table>

Code for Controller

 public class PaymentsController : Controller
    {
        private readonly ConnectionString _context;

        public PaymentsController(ConnectionString context)
        {
            _context = context;
        }
 public IActionResult Wcreate(Welfare welfare, string A)
        {
            int maxR_No = _context.Welfare.Max(p => p.R_No);
            maxR_No = maxR_No + 1;
            welfare.R_No = maxR_No;

            if (ModelState.IsValid)
            {

                _context.Add(welfare);
                _context.SaveChanges();
                ModelState.Clear();
            }
            return View();
        }
 public ActionResult getStudentInfo(string A)
        {
            var items = _context.Students.Where(x => x.Add_No == A)
              .Select(x => new
              {
                  P1 = x.F_Name,
                  P2 = x.Class + "-" + x.ClassNo
              });//.ToList();

            ViewData["students"] = items;
            return RedirectToAction("Wcreate");
        }

namespace Student_Management.Models
{
    public class Welfare
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int R_No { get; set; }
        [Required]
        public string Add_No { get; set; }
        [Required]
        public string Grade { get; set; }
        [Required]
        public string STClassNo { get; set; }
        [Required]
        public string Month { get; set; }
        [Required]
        public string Year { get; set; }
        [Required]
        public string Rs { get; set; }
        [Required]
        [DataType(DataType.Date)]
        public DateTime Date { get; set; }
        public string cmt { get; set; }
Community
  • 1
  • 1
chandrasiri
  • 83
  • 1
  • 3
  • 11

1 Answers1

0

As hint given by Jasen, I modified my action method and view as follows.

<td>
            @using (Html.BeginForm("getStudentInfo", "Payments", FormMethod.Post))
            {
                <div class="form-group">
                    <label asp-for="Add_No" class="control-label"></label>
                    <input asp-for="Add_No" class="form-control" id="t1" name="A" />
                    <span asp-validation-for="Add_No" class="text-danger"></span>
                </div>

                <div class="form-group">
                    <input type="submit" value="getStudentInfo" class="btn btn-primary" id="btngetStudentInfo" />
                </div>
            }
        </td>
        <p>@TempData["info"]</p>
        <p>@(TempData["info2"] +"-" + TempData["info3"])</p>

Action method

 public ActionResult getStudentInfo(string A)
        {
            var items = _context.Students.Where(x => x.Add_No == A)
              .Select(x => new
              {
                  P1 = x.F_Name,
                  P2 = x.Class,
                  p3=x.ClassNo
              }).ToList();
            TempData["info"] = items[0].P1; 
TempData["info2"] = items[0].P2; 
TempData["info3"] = items[0].p3;
            return RedirectToAction("Wcreate");
        }

chandrasiri
  • 83
  • 1
  • 3
  • 11
  • But anyone can please explain how can we pass a rowset retrieved from database to a view from an action method which is using --- return RedirectToAction("AnotherAction") --- – chandrasiri Feb 08 '20 at 04:01