我無法在同一個控制器的GET方法中獲得模型和它的值,該方法由POST傳遞。請看下面的代碼片段。請幫助我解決這個問題。
模型:
public class country
{
public string CountryCode { get; set; }
public long? CountryId { get; set; }
控制器:
[HttpGet]
public async Task<ActionResult> Create()
{
return View()。
}
[HttpPost]
public async Task< ActionResult> Create(Country _country)
{
var data = await checkduplicate(_country)。
if (! data.status)
{
ModelState.AddModelError("Name"/span>, data.ErrorMessage)。
return RedirectToAction("Edit", new {_country}) 。
}
else; }
{
return RedirectToAction("Index"/span>)。
}
}
public async Task< ActionResult> Edit(Country _country)
{
return View("Create", _country);
}
在這個例子中,我只給了兩個值,但實際我有大約8個引數。請提出建議。我已經使用了Razor視圖頁面。
提前感謝。
uj5u.com熱心網友回復:
我得到了答案,這是一個小錯誤,當通過后端發送模型時。 它應該如下所示。我在其中加入了關鍵詞 "new"。在洗掉關鍵字new后,它作業得很好。
[HttpPost]
public async Task<ActionResult> Create(Country _country)
{
var data = await checkduplicate(_country)。
if (! data.status)
{
ModelState.AddModelError("Name"/span>, data.ErrorMessage)。
return RedirectToAction("Edit", _country) 。
}
else[/span
{
return RedirectToAction("Index") 。
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/319219.html
標籤:
上一篇:從視圖中的索賠中讀取數值
