InvalidOperationException:未找到视图“AddValue”。提交表单数据时未找到部分视图

问题描述 投票:0回答:1

我正在尝试从单个视图页面将数据插入到两个表中。我使用了两种不同的视图模型来做到这一点。所以我必须制作一个部分视图来在第二个表中插入数据。现在完整页面已打开,第一部分工作成功。虽然部分视图已打开,但当我提交数据时,它显示此错误:

InvalidOperationException:未找到视图“AddValue”。搜索了以下位置:
/视图/属性/AddValue.cshtml
/Views/Shared/AddValue.cshtml
/Pages/Shared/AddValue.cshtml

这是控制器代码:

public class AttributeController : Controller
{
    private readonly AttributeInfoService _attributeService;
    private readonly AttributeValueService _valueService;
    public AttributeController(IAttributeInfoRepository attributeRepository, IAttributeValueRepository valueRepository)
    {
        this._attributeService= new AttributeInfoService(attributeRepository);
        this._valueService= new AttributeValueService(valueRepository);
    }

    [HttpGet]
    public IActionResult ViewAttribute()
    {
        return View(_attributeService.GetAttributeList());
    }

    [HttpGet]
    public IActionResult AddAttribute()
    {
        ViewBag.attribute = _attributeService.AttributeDropDown();
        return View();
    }

    [HttpPost]
    public IActionResult AddAttribute(AttributeInfoViewModel avm)
    {
        if(ModelState.IsValid)
        {
            string status = _attributeService.Add(avm);
            if (status == "OK")
            {
                return RedirectToAction("AddAttribute");
            }
            
        }
        else {
            string msg = "fail";
            ViewBag.Message = msg;
        }
        
        return View(avm);
    }

    [HttpGet]
    public IActionResult EditAttribute()
    {
        return View();
    }

    [HttpPost]
    public IActionResult EditAttribute(AttributeInfoViewModel avm)
    {
        if (ModelState.IsValid)
        {
            var status= _attributeService.Update(avm);
            if(status == "OK")
            {
                return RedirectToAction("");
            }
        }
        return View(avm);
    }

    [HttpPost]
    public IActionResult DeleteAttribute(int id)
    {
        if (id != 0)
        {
            _attributeService.Delete(id);
            return RedirectToAction("");
        }
        return View();
    }


    [HttpGet]
    public IActionResult ViewValue()
    {
        return View(_valueService.GetAllAttributeValueList());
    }
    
    [HttpPost]
    public IActionResult AddValue(AttributeValueViewModel avm)
    {
        if (ModelState.IsValid)
        {
            string status = _valueService.Add(avm);
            if (status == "OK")
            {
                return RedirectToAction("AddValue");
            }
        }
        return View(avm);
    }

    [HttpGet]
    public IActionResult EditValue()
    {
        return View();
    }

    [HttpPost]
    public IActionResult EditValue(AttributeValueViewModel vm)
    {
        if (ModelState.IsValid)
        {
            string status = _valueService.Update(vm);
            if( status == "OK")
            {
                return RedirectToAction("");
            }
        }
        return View(vm);
    }

    [HttpPost]
    public IActionResult DeleteValue(int id)
    {
        if(id!=0)
        {
            _valueService.Delete(id);
            return RedirectToAction("");
        }
        return View();
    }
}

这里是查看代码:

@model DL.ViewModels.Configuration.AttributeInfoViewModel
@{
    ViewData["Title"] = "AddAttribute";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="d-flex justify-content-between mb-4">
    <div class="">
        <h4 class="text_primary fw-bold">Add Product Attribute</h4>
    </div>
    <div class="d-flex bg_primary rounded-2 overflow-hidden">
        <a title="Sale Order List" asp-action="AddProductInfo" asp-controller="Inventory"
           class="my_button px-2 py-1 rounded-0">
            Add Product
        </a>

    </div>
</div>

<form asp-action="AddAttribute" asp-controller="Attribute" method="post">
    <div class="sale_order_container rounded-2 mb-4">
        <div class="">
            <h6 class="border-bottom border-secondary-subtle soc_title py-3 px-4">
                Attribute @ViewBag.Message
            </h6>
        </div>
        <div class="grid_cols_2 p-4 ">
            <div class="">
                <label asp-for="AttributeName" class="form-label text-secondary">Attribute Name</label>
                <input type="text"
                       asp-for="AttributeName"
                       id="AttributeName"
                       class="form-control shadow-none rounded-0" />
            </div>
            <div class="">
                <label asp-for="Description" class="form-label text-secondary">Description</label>
                <input type="text"
                       asp-for="Description"
                       id="Description"
                       class="form-control shadow-none rounded-0" />
            </div>


        </div>
        <div class="col-md-2 mx-auto pb-4">
            <button type="submit" class="sol_add_button mt-0 w-100">
                <i role="button"
                   class="fa-solid fa-floppy-disk fa-fw fw-bolder fs-5"></i>
                <span>Save</span>
            </button>
        </div>
    </div>
</form>

@await Html.PartialAsync("_AddValue")
@* <partial name="_AddValue.cshtml"/> *@


@section Scripts {
    <script>
        $("#left_Configuration_menu").show();
    </script>
}

这是部分视图代码:

@model DL.ViewModels.Configuration.AttributeValueViewModel

<form action="AddValue" method="post">
    <div class="sale_order_container rounded-2 mb-4">
        <div class="">
            <h6 class="border-bottom border-secondary-subtle soc_title py-3 px-4">
                Attribute Value
            </h6>
        </div>
        <div class="grid_cols_3 p-4 ">
            <div class="">
                <label for="AttributeId" class="form-label text-secondary">Attribute Id</label>
                <select asp-for="AttributeId"
                        asp-items="@ViewBag.attribute"
                        id="AttributeId"
                        class="form-select shadow-none rounded-0">
                    <option value="" selected>Select Attribute</option>
                </select>
            </div>
            <div class="">
                <label asp-for="AttributeValueName" class="form-label text-secondary">Attribute Value</label>
                <input type="text"
                       asp-for="AttributeValueName"
                       id="Description"
                       class="form-control shadow-none rounded-0" />
            </div>
            <div class="">
                <label asp-for="Description" class="form-label text-secondary">Description</label>
                <input type="text"
                       asp-for="Description"
                       id="Description"
                       class="form-control shadow-none rounded-0" />
            </div>


        </div>
        <div class="col-md-2 mx-auto pb-4">
            <button type="submit" class="sol_add_button mt-0 w-100">
                <i role="button"
                   class="fa-solid fa-floppy-disk fa-fw fw-bolder fs-5"></i>
                <span>Save</span>
            </button>
        </div>
    </div>
</form>
asp.net asp.net-mvc asp.net-core razor partial-views
1个回答
0
投票

您的 PartialView 需要

AttributeValueViewModel
。您需要将其传递给
PartialAsync
方法。

@await Html.PartialAsync("_AddValue", new AttributeValueViewModel())

类似上面的东西。显然,您不会在这里创建新对象,而是必须在 Controller 中创建。

© www.soinside.com 2019 - 2024. All rights reserved.