如何修复错误“必须先绑定数据源,然后才能执行此操作”

时间:2019-11-22 22:35:54

标签: c# asp.net-mvc razor

我知道这个问题已经问过几次了,但都不适合我为什么收到此错误。从视图中,我将两个表单值发布到Controller,在其中运行查询并以列表形式返回结果。如何解决此错误?

ViewModel:

public class ScannerViewModel
{
    public string JobNumber { get; set; }
    public string ProgramName { get; set; }
    public string ItemCode2 { get; set; }
}

控制器:

[HttpPost]
public ActionResult ScannerEdit(string jobNumber)
{
    try
    {
        List<ScannerViewModel> list = new List<ScannerViewModel>();
        using (Scanner ctx = new Scanner())
        {
            var v = (from s in ctx.tScanners
                    where s.JobNumber == jobNumber
                    select new ScannerViewModel
                    {
                        ProgramName = s.ProgramName,
                        JobNumber = s.JobNumber,
                        ItemCode2 = s.ItemCode2
                    });
            list = v.ToList();
        }

        return View(list);

    }
    catch (Exception e)
    {
        Debug.WriteLine("ERROR: " + e.Message);
        return Json(new { status = "error", message = e.Message }, JsonRequestBehavior.AllowGet);
        throw;
    }
}

查看:

@model List<FulfillmentPortal.ViewModels.ScannerViewModel>
@{
ViewBag.Title = "ScannerEdit";
Layout = "~/Views/Shared/_Layout.cshtml";
WebGrid grid = new WebGrid(Model, rowsPerPage:10);
}

<h2>Edit Scanner Job</h2>

<div id="wrapper">
<div class="panel panel-primary">
<div class="panel-heading">EDIT SCANNER JOB</div>
<div class="panel-body" style="padding-left:35px;">
    <form id="processForm" class="form-horizontal">
        <div class="form-group">
            @Html.Label("Program Name:")
            @Html.DropDownList("programName", new SelectList(string.Empty, "Value", "Text"), new { id = "programName", @class = "form-control", @style = "width: auto; margin: 0 0 25px 0;" })

            @Html.Label("Job Number:")
            @Html.DropDownList("jobNumber", new SelectList(string.Empty, "Value", "Text"), new { id = "jobNumber", @class = "form-control", @style = "width: auto; margin: 0 0 25px 0;" })
        </div>
        <div class="grid-alter-row">
            @grid.GetHtml(tableStyle: "table table-responsive table-striped table-bordered",
                columns: grid.Columns(
                    grid.Column(header: "Program Name", format: @<text><div class="" data-propertyname="ProgramName">@item.ProgramName</div></text>),
                    grid.Column(header: "Job Number", format:@<text><div class="" data-propertyname="JobNumber">@item.JobNumber</div></text>),
                    grid.Column(header: "Itemcode 2", format:@<text><div class="editCode" data-id="@item.JobNumber" data-propertyname="ItemCode2">@item.ItemCode2</div></text>)
                )
            )
        </div>
    </form>
</div>
</div>

2 个答案:

答案 0 :(得分:0)

我认为您的问题可能在两个下拉列表中:

     @Html.DropDownList("programName", new SelectList(string.Empty, "Value", "Text"), new { id = "programName", @class = "form-control", @style = "width: auto; margin: 0 0 25px 0;" })
     @Html.DropDownList("jobNumber", new SelectList(string.Empty, "Value", "Text"), new { id = "jobNumber", @class = "form-control", @style = "width: auto; margin: 0 0 25px 0;" })

因为您使用的是new,并且其中没有数据。

答案 1 :(得分:0)

发生错误是因为第二个@ Html.DropDownList通过ajax调用调用了控制器方法。导致视图加载时模型不存在。