我创建了一个页面,用户搜索书籍,并使用局部视图加载书籍详细信息。我成功地做到了这一点。 控制器中的操作方法用于执行此操作:
[HttpGet]
public ActionResult LoanSearch(string q)
{
var loans = GetLoans(q);
return PartialView(loans);
}
private List<Loan> GetLoans(string searchString)
{
return db.Loans
.Where(a => a.Book.Name.Contains(searchString))
.ToList();
}
正如您所看到的,LoanSearch操作方法是用HTTPGET修饰的。
观点:
@using (Html.BeginForm())
{
foreach (var item in Model)
{
<ul>
<li>@item.ISBN</li>
<li>@item.Book.Name</li>
<li>@item.Book.Author</li>
<li> @item.FinePrice</li>
</ul>
@Html.ActionLink("Return Book", "LoanSearch", new { id = item.LoanId });
}
}
我想要做的是更新贷款数据库中的finePrice并将书籍数据库中的onLoan从1更改为0。当用户点击上面的上述Html.ActionLink时,就会发生这种情况。
为了实现这一点,我创建了以下HTTPPOST操作方法,并且还使用了视图模型,因为我需要同时更新2个表(Loan,Book)。(需要视图模型吗?)
[HttpPost]
public ActionResult LoanSearch(BookReturnVM model, string searchString)
{
var bookquery = db.Loans.Where(a => a.Book.Name.Contains(searchString));
var loanquery = db.Loans.Where(a => a.Book.Name == model.BookTitle);
var finePrice = db.Loans.Where(g => g.FinePrice == model.FinePrice);
BookReturnVM model1 = new BookReturnVM
{
OnLoan = model.OnLoan,
FinePrice = model.FinePrice,
};
if (ModelState.IsValid)
{
var fine = db.Loans.FirstOrDefault(g => g.FinePrice == model.FinePrice);
var bookLoan = db.Loans.FirstOrDefault(a => a.Book.Name.Contains(searchString));
if (bookLoan != null)
{ //changes the onloan status to 1 which makes it 'on loan'
bookLoan.Book.OnLoan = 0;
};
db.Entry(bookLoan).State = EntityState.Modified;
db.SaveChanges();
}
return View();
我希望根据下面的计算更新贷款表中的finePrice。 finePrice计算在贷款模型中完成:
private decimal? _FinePrice;
public decimal? FinePrice
{
get
{
if(DateTime.Now>CheckOutDate)
{
this._FinePrice= ((DateTime.Now - CheckOutDate).Days)*0.50M;
}
else
{
this._FinePrice = 0M;
}
return this._FinePrice;
}
set
{
this._FinePrice = value;
}
}
总之,当我搜索一本书时,会出现该书的详细信息(在局部视图中,它会这样做),当我点击一个按钮(在同一部分视图页面上)时,OnLoan(书籍表)会从1到0,并为该特定书籍更新finePrice(贷款表)。
我认为问题出在HTTP POST操作方法中,但不知道如何解决这个问题以解决上述问题。
感谢您的时间
答案 0 :(得分:0)
您可以这样做:
表:
USE [Breaz]
GO
/****** Object: Table [dbo].[Loan] Script Date: 6/26/2017 11:15:15 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Loan](
[LoanId] [int] IDENTITY(1,1) NOT NULL,
[ISBN] [varchar](20) NULL,
[Name] [varchar](20) NULL,
[Author] [varchar](20) NULL,
[FinePrice] [money] NOT NULL,
CONSTRAINT [PK_Loan] PRIMARY KEY CLUSTERED
(
[LoanId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
控制器
public class HomeController : Controller
{
[HttpGet]
public PartialViewResult LoanSearchByIdAndUpdate(int id)
{
//DO your database update here
//I will change the find to 2.37
Loan loan = new Loan();
var bookName = String.Empty;
using (BreazEntities26 db = new BreazEntities26())
{
loan = db.Loans.Find(id);
loan.FinePrice = 2.37M;
bookName = loan.Name;
}
IList<Loan> loans = new List<Loan>();
loans.Add(loan);
return PartialView("_LoanSearch", loans););
}
[HttpGet]
public PartialViewResult LoanSearch(string q)
{
var loans = GetLoans(q);
return PartialView("_LoanSearch", loans););
}
private List<Loan> GetLoans(string searchString)
{
using (BreazEntities26 db = new BreazEntities26())
{
return db.Loans
.Where(a => a.Name.Contains(searchString))
.ToList();
}
}
IndexValid2.cshtml
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>IndexValid2</title>
<script src="~/Scripts/jquery-1.12.4.min.js"></script>
@*MAKE SURE to put the next script in*@
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
<script type="text/javascript">
$(function () {
$('#passSearchValue').click(function () {
var searchValue = $('#q').val();
this.href = this.href + '?q=' + encodeURIComponent(searchValue);
});
})
</script>
</head>
<body>
<div>
Search for book:
@Html.TextBox("q", null, new { id = "q" })
@*https://stackoverflow.com/questions/5838273/actionlink-routevalue-from-a-textbox*@
@Ajax.ActionLink(
"Search",
"LoanSearch",
null,
new AjaxOptions
{
UpdateTargetId = "result",
InsertionMode = InsertionMode.Replace,
HttpMethod = "GET"
},
new { id = "passSearchValue" })
</div>
<div id="result"></div>
</body>
</html>
在shared,_LoanSearch.cshtml中的部分视图
@model IEnumerable<Testy20161006.Models.Loan>
@foreach (var item in Model)
{
<ul>
<li>@item.ISBN</li>
<li>@item.Name</li>
<li>@item.Author</li>
<li> @item.FinePrice</li>
</ul>
@Html.ActionLink("Update", "LoanSearchByIdAndUpdate", new { id = item.LoanId });
<p />
@Html.ActionLink("Return to Search", "IndexValid2");
}