skip to Main Content

I’m trying to insert data into two table from a single View Page. I’ve used two different view model to do that. So I have to make a partial view for inserting data in second table. Now full page is opening and 1st part working successfully. Though partial view is opening but when I submit data it’s showing this error:

InvalidOperationException: The view ‘AddValue’ was not found. The following locations were searched:
/Views/Attribute/AddValue.cshtml
/Views/Shared/AddValue.cshtml
/Pages/Shared/AddValue.cshtml

Here is the controller code:

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();
    }
}

Here is the View Code for:

@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>
}

Here is the Partial View Code:

@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>

2

Answers


  1. Your PartialView expects AttributeValueViewModel. You need to pass it to PartialAsync method.

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

    Something like above.

    Login or Signup to reply.
  2. When you called the action AddValue, it will return the default view with the same name as the method, so it will find the view named AddValue.cshtml
    Please check the folders to confirm that you have created the view named AddValue.cshtml, and if the view you created is not under the folder: /Views/Attribute, /Views/Shared, /Pages/Shared, you can directly specify the view name in the return statement.

    1. Or if you want to return the partial view “_ AddValue.cshtml”, you can modify your return statement:

    enter image description here

    1. Or you want to return the original view page named “AddAttribute.cshtml”, you can modify you code like this:

    enter image description here

    But there’s a catch with this approach. When you execute the AddValue operation, if the if statement fails, it will execute ‘return RedirectToAction("AddAttribute")’. At this point, your data ‘avm’ won’t be passed to the view.

    And if you want to avoid this scenario, you can use AJAX in the partial view.
    And in your AddAttribute.cshtml: Your PartialView expects AttributeValueViewModel. You can pass it to PartialAsync method.
    @await Html.PartialAsync("_AddValue", new AttributeValueViewModel())

    Would this solution help you resolve the issue?
    If not, could you provide me with more specific details about your requirement? I’ll further assist you in resolving this issue

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search