ActionLink不会将参数传递给控制器​​功能

时间:2019-06-16 21:48:52

标签: c# .net asp.net-mvc

我想使用ActionLink在视图内部调用一个函数,该函数在调用它时运行,URL应该显示出来,但是我在视图内部提供的用于调用该函数的值未显示。 / p>

控制器:

public async Task<ActionResult> LoanPayAmount(string loanId, double paidAmount)
        {
            Debug.WriteLine("Loan ID", loanId);
            bool success = false;
            CustomerManager profileManager = new CustomerManager();
            ApplicationUser customer = await profileManager.RequestCustomerProfile(System.Web.HttpContext.Current.User.Identity.Name);
            ApplicationDbContext context = new ApplicationDbContext();
            LoanModel loanAccount = await profileManager.RequestLoanAccount(loanId);
            ViewData["Loan"] = loanAccount;
            Debug.WriteLine(loanAccount.LoanId);
            try
            {

                if (loanAccount != null)
                {
                    Debug.WriteLine("Not null");
                    loanAccount = context.Loans.Where(c => c.User.UserName == System.Web.HttpContext.Current.User.Identity.Name && c.LoanId == loanId).FirstOrDefault();
                    loanAccount.LoanAmountPaidThisMonth += paidAmount;
                    loanAccount.LoanAmountPaid += paidAmount;
                }
                else
                {
                    success = false;
                }
            }
            catch (Exception)
            {
                success = false;
            }
            if (success)
            {
                ViewBag.StatusMessage = String.Format("Amount Paid");
            }
            else
            {
                ViewBag.StatusMessage = String.Format("Amount not paid");
            }
            //return Redirect("~/");
            return View();
        }

查看:

@try
{
    foreach (var acc in Model.Loans)
    {
        <table class="table table-bordered table-hover">
            <tr>
                <td class="c">Loan Account Number: </td>
                <td class="c">@acc.LoanId</td>
            </tr>
            <tr>
                <td class="c">Paid Amount: </td>
                <td class="c">@acc.LoanAmountPaid / @acc.AmountToPay</td>
            </tr>
            <tr>
                <td class="c">Interest Rate: </td>
                <td class="c">@acc.InterestRate%</td>
            </tr>
            <tr>
                <td class="c">Deadline: </td>
                <td class="c">@acc.LoanDeadline</td>
            </tr>
            <tr>
                <td class="c">Amount paid this month: </td>
                <td class="c">@acc.LoanAmountPaidThisMonth / @String.Format("{0:0.00}", acc.LoanMonthlyAmount)</td>
            </tr>
            <tr>
                <td class="c">Pay Monthly</td>
                <td class="c"><input type="text" name="LoanAmountPaidThisMonth" id="LoanAmountPaidThisMonth" /></td>
            </tr>
        </table>
        @Html.ActionLink("Pay!", "LoanPayAmount", "Loan", new { id = acc.LoanId, paidAmount = 23 }, null)
    }
}
catch
{
    <h6 style="color:black;">No loans here.</h6>
}

型号:

public class LoanModel
    {
        [Key]
        public string LoanId { get; set; }
        public string UserId { get; set; }
        [ForeignKey("UserId")]
        public virtual ApplicationUser User { get; set; }
        public string LoanOfficerId { get; set; }
        public double LoanAmount { get; set; }
        public double AmountToPay { get; set; }
        public double InterestRate { get; set; }
        public double LoanAmountPaid { get; set; }
        public double LoanAmountPaidThisMonth { get; set; }
        public double LoanMonthlyAmount { get; set; }
        public string LoanDate { get; set; }
        [Display(Name = "Loan Period:"), Required]
        public string LoanDeadline { get; set; }
        public string LoanOfficerApproval { get; set; }
        public string Status { get; set; }
        [Display(Name = "Loan Cause:"), Required]
        public string LoanCause { get; set; }
    }

RouteConfig:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                     name: "LoanPay",
                     url: "Loan/LoanPayAmount/{loanId}/{paidAmount}",
                     defaults: new { controller = "Loan", action = "LoanPayAmount", loanId = UrlParameter.Optional, paidAmount = UrlParameter.Optional }
                 );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
        }

我希望能够更新 Loan LoanAmountPaidThisMonth ,但是没有任何变化,我尝试打印出我想传递给控制者的参数,但是什么都没有出现,所以我想问题出在视图内调用函数时。

0 个答案:

没有答案