【问题标题】:Model gets NULL while passing as parameter from POST method to GET method of same controller in ASP.NET MVC using C#模型在使用 C# 将 POST 方法作为参数传递给 ASP.NET MVC 中同一控制器的 GET 方法时获取 NULL
【发布时间】:2021-10-20 07:48:30
【问题描述】:

我无法在同一控制器的POST 传递的GET 方法中获取模型及其值。请在下面找到代码 sn-p。请帮我解决它。

型号:

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", data.ErrorMessage);
        return RedirectToAction("Edit", new {_country});
    }
    else
    {
        return RedirectToAction("Index");
    }
}

public async Task<ActionResult> Edit(Country _country)
{
    return View("Create", _country);
}

在这个示例中,我只给出了两个值,但实际上我有大约 8 个参数。请提出建议。我用的是razor view page。

提前致谢。

【问题讨论】:

  • 请发布您尝试提交的整个视图。没有这个,没有人可以帮助你

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


【解决方案1】:

我得到了答案,这是一个小错误,在通过 post end 发送模型时。 它应该如下所示。我在其中包含了关键字“new”。删除关键字 new 后,它工作正常。

[HttpPost]
public async Task<ActionResult> Create(Country _country)
{
    var data = await checkduplicate(_country);

    if (!data.status)
    {
        ModelState.AddModelError("Name", data.ErrorMessage);
        return RedirectToAction("Edit", _country);
    }
    else
    {
        return RedirectToAction("Index");
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 2016-05-08
    相关资源
    最近更新 更多