我正在尝试将值从控制器传递到查看页面,但它显示我的控制器正在接收空值,即使我在 link 中传递它

I am trying to pass value from controller to view page but it shows my controller is receiving null value, even though I'm passing it in the link

我试图通过接受“val”中的值来执行控制器。通过“ID”,我试图从数据库中获取准确的数据。然后我将检索数据传递到我的视图页面,以便显示正确的数据值及其在 Modal class.

中定义的相关名称

这是我的网页在选择“用户详细信息”后显示的错误

Page view before clicking User Details

Page view after clicking User Details

我收到以下错误:

Server Error in '/' Application. The parameters dictionary contains a null entry for parameter 'val' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult UserDetails(Int32)' in 'OfficeWork.Controllers.PopulationsController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

代码主页控制器:

private PopulationDBContext db = new PopulationDBContext();

public ActionResult HomePage(int? id)
{
    Population population = db.Populations.Find(id);
    return View(population);
}

UserDetails 页面控制器代码:

private PopulationDBContext db = new PopulationDBContext();
public ActionResult UserDetails(int val)
{
    var std = db.Populations.Where(s => s.ID == val).FirstOrDefault();      
    return View(std);
}

型号class:

using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;

namespace OfficeWork.Models
{
    public class Population
    {
        [Key]
        public int ID { get; set; }
        [Required]
        [Display(Name ="Email")]
        [EmailAddress(ErrorMessage ="Enter valid email address")]
        public string Email { get; set; }
        [StringLength(10)]
        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }
        [StringLength(10)]
        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }
        [Display(Name = "Mobile Number")]
        [Required]
        public long MobileNumber { get; set; }
        [Display(Name = "Date Of Birth")]
        [Required]
        public DateTime DateOfBirth { get; set; }
        [Display(Name = "Password")]
        [Required]
        public string Password { get; set; }
    }

    public class PopulationDBContext : DbContext
    {
        public DbSet<Population> Populations { get; set; }
    }
}

HomePage.cshtml

@model OfficeWork.Models.Population
@{
    ViewBag.Title = "HomePage";
}
@{
    var value = Model.ID;
}

@section navbar{
    <div class="container-fluid">
        <div class="navbar-header">
            <P class="navbar-brand">PROJECT</P>
        </div>
        <ul class="nav navbar-nav">
            <li class="active" style="cursor:pointer;"><a>Home</a></li>
            <li>@Html.ActionLink("User Details", "UserDetails/"+value, new { controller = "Populations" }, htmlAttributes: new { @style = "cursor:pointer;" })</li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li>@Html.ActionLink(" Log Out", "SignIn", "populations", htmlAttributes: new { @class = "glyphicon glyphicon-user" })</li>
        </ul>
    </div>
}

<br />
<br />
<div class="container">
    <img src="~/Images/forestbridge.jpg" alt="Notebook" style="width:100%;">
    <div class="centered">Coming Soon...</div>
    <div class="content">
        <h1>Hello There!</h1>
        <p>While it’s difficult to be entirely optimistic in this unprecedented times, we must believe, life is short, but it is wide.“this too shall pass”.</p>
    </div>
</div>
@Html.Hidden("studentIdVal", @value);

UserDetails.cshtml

@model OfficeWork.Models.Population

@{
    ViewBag.Title = "Main";
}

@{
    var value = Model.ID;
}

@*Nav bar to direct user to Log Out page(Log In Page)*@

@section navbar{
    <div class="container-fluid">
        <div class="navbar-header">
            <P class="navbar-brand">PROJECT</P>
        </div>
        <ul class="nav navbar-nav">
            <li>@Html.ActionLink("Home", "HomePage/" + value, new { controller = "Populations" }, htmlAttributes: new { @style = "cursor:pointer;" })</li>
            <li class="active" style="cursor:pointer;"><a>User Details</a></li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li>@Html.ActionLink(" Log Out", "SignIn", "populations", htmlAttributes: new { @class = "glyphicon glyphicon-user" })</li>
        </ul>
    </div>
}

<div>
    <br />
    <br />
    <h4>User Details</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Email)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Email)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.FirstName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.FirstName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.LastName)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.LastName)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.MobileNumber)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.MobileNumber)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.DateOfBirth)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.DateOfBirth)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Password)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Password)
        </dd>
    </dl>
</div>
<p>
</p>

您使用的 LinkExtensions.ActionLink

的重载不正确

您应该使用的重载是 this,即

public static System.Web.Mvc.MvcHtmlString ActionLink (this System.Web.Mvc.HtmlHelper htmlHelper, string linkText, string actionName, string controllerName, System.Web.Routing.RouteValueDictionary routeValues, System.Collections.Generic.IDictionary<string,object> htmlAttributes);

所以你的代码应该是:

@Html.ActionLink("User Details", "UserDetails", "Populations", new { val = value }, new { @style = "cursor:pointer;" })

您可以使用 intellisense 验证自己实际使用的是哪个超载,但我怀疑它是 this 一个,即:

public static System.Web.Mvc.MvcHtmlString ActionLink (this System.Web.Mvc.HtmlHelper htmlHelper, string linkText, string actionName, object routeValues, object htmlAttributes);

所以 { controller = "Populations" } 被解释为路由值。您希望将 { val = value } 作为路由值传递。