简体   繁体   English

在一个 razor 视图中创建多个模型(ASP.NET Core)

[英]Multiple create models in one razor view (ASP.NET Core)

I have multiple create forms in my view and they always send null data to the controller.在我看来,我有多个create forms 并且他们总是将 null 数据发送到 controller。

Here's my code View model:这是我的代码查看 model:

public class IndexModel : PageModel
{
    public List<Device> devicelist { get; set; }
    public Process proclist { get; set; }
    public Extratime extras { get; set; }
    public Foodadd foods { get; set; }

    public void OnGet()
    {
        devicelist = new List<Device>();
        proclist = new Process();
        extras = new Extratime();
        foods = new Foodadd();
    }
}

And one of the forms in my view which always sends null data:在我看来,其中一个 forms 总是发送 null 数据:

<form method="post" asp-action="AddTime" style="direction: rtl;">
    <input type="hidden" asp-for="@Model.proclist.DeviceId" value="@item.Id" />
    <input type="hidden" asp-for="@Model.proclist.Userid" value="@currentUser.Id" />
    <div class="form-group">
        <select asp-for="@Model.proclist.IsLimited" class="form-control" id="cc">
            <option value="true">limited</option>
            <option value="false">Open</option>
        </select>
    </div>
    <div class="form-group" id="pp">
        <input type="number" asp-for="@Model.proclist.hours" class="form-control home" placeholder="1.5" step=".01">
    </div>
    <div class="form-group">
        <select asp-for="@Model.proclist.IsSingle" class="form-control">
            <option value="true">single</option>
            <option value="false">multi</option>
        </select>
    </div>

    <button type="submit" class="btn btn-primary">Enable</button>
</form>

That's what my controller expects from my view ( returns a NullReferenceException for the lack of data from the view. ):这就是我的 controller 从我的视图中所期望的(由于视图中缺少数据而返回 NullReferenceException。):

public IActionResult AddTime(Process data)
    {
        using (KoraContext db = new KoraContext())
        {
            var tdevice = db.Devices.Where(x => x.Id == data.DeviceId).FirstOrDefault();
            tdevice.Isbusy = "true"; // Gives a NullReferenceException.

            db.Processes.Add(data);
            db.SaveChanges();

            return RedirectToAction("index", new { Controller = "Home", Action = "Index" });
        }
    }

You have a form with method="post" but your page model doesn't have OnPost method.您有一个带有 method="post" 的表单,但您的页面 model 没有 OnPost 方法。 You also need to add [BindProperty] attribute:您还需要添加[BindProperty]属性:

public class IndexModel : PageModel
{
    public List<Device> devicelist { get; set; }

    [BindProperty]
    public Process proclist { get; set; }
    public Extratime extras { get; set; }
    public Foodadd foods { get; set; }

    public void OnGet()
    {
        devicelist = new List<Device>();
        proclist = new Process();
        extras = new Extratime();
        foods = new Foodadd();
    }

    public void OnPost()
    {
        // do something with proclist
    }
}

https://www.learnrazorpages.com/razor-pages/forms https://www.learnrazorpages.com/razor-pages/forms

I think you misunderstand the way of using Razor Page .我想你误解了使用Razor Page的方式。 In Razor Page , There is no Controller in Backstage, So if you want to set HttpGet or HttpPost action, you need to set the method name like OnGetxxxx and OnPostxxx (xxx is method Name), Then if you want to pass model between PageModel and View , You need to add [BindProperty] , So in your PageModel, You need to change the code like: In Razor Page , There is no Controller in Backstage, So if you want to set HttpGet or HttpPost action, you need to set the method name like OnGetxxxx and OnPostxxx (xxx is method Name), Then if you want to pass model between PageModel and View ,您需要添加[BindProperty] ,因此在您的 PageModel 中,您需要更改以下代码:

public class IndexModel : PageModel
{
    public List<Device> devicelist { get; set; }
    [BindProperty]
    public Process proclist { get; set; }
    public Extratime extras { get; set; }
    public Foodadd foods { get; set; }

    public void OnGet()
    {
       devicelist = new List<Device>();
       proclist = new Process();
       extras = new Extratime();
       foods = new Foodadd();
   }

    public IActionResult OnPostAddTime()
    {
        using (KoraContext db = new KoraContext())
        {
            var tdevice = db.Devices.Where(x => x.Id == proclist.DeviceId).FirstOrDefault();
            tdevice.Isbusy = "true"; // Gives a NullReferenceException.

            db.Processes.Add(proclist);
            db.SaveChanges();

            return RedirectToPage(xxxxxxx);
        }
    }

}

Then in the view, You can use asp-page-handler=xxx to specify whtich method you want to submit.然后在视图中,您可以使用asp-page-handler=xxx来指定要提交的方法。

More information you can refer to this link .更多信息可以参考这个链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM