Я хочу вызвать функцию внутри представления, используя ActionLink, функция запускается при ее вызове, URL-адрес отображается так, как должен, но значения, которые я предоставляю внутри представления для вызова функции, не отображаются.
Контроллер:
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 , но ничего не изменилось, я попытался распечатать параметр, который пытаюсь передать контроллеру, но ничего не появляется, поэтому я полагаю, что проблема заключается в вызове функции внутри представления.