简体   繁体   中英

ASP.NET Core Razor using property as base for post form

@model Tuple<ApplicationsModel, AR.WebShop.Product<AR.TDShop.ProductCustomProperties>>

<div style="padding: 20px">
    <form method="post" action="/TDShop/AddNewProduct">
        @Html.EditorFor(x => x.Item2.ID);
        @Html.EditorFor(x => x.Item2.Rel);
        @Html.EditorFor(x => x.Item2.CatalogueID);
        @Html.EditorFor(x => x.Item2.Name);
        @Html.EditorFor(x => x.Item2.Thumbnail);
        @Html.EditorFor(x => x.Item2.SubGroupID);
        @Html.EditorFor(x => x.Item2.Unit);
        @Html.EditorFor(x => x.Item2.Active);
        @Html.EditorFor(x => x.Item2.VAT);
        @Html.EditorFor(x => x.Item2.DisplayIndex);
        @Html.EditorFor(x => x.Item2.ShortDescription);
        @Html.EditorFor(x => x.Item2.LongDescription);
        @Html.EditorFor(x => x.Item2.Sale);
        @Html.EditorFor(x => x.Item2.Keywords);
        @Html.EditorFor(x => x.Item2.Price);
        @Html.EditorFor(x => x.Item2.PurchasePrice);
        @Html.EditorFor(x => x.Item2.Tag.Klasifikacija);
        @Html.EditorFor(x => x.Item2.Tag.TransportnoPakovanje);
        @Html.EditorFor(x => x.Item2.Tag.TransportnoPakovanjeJM);
        @Html.EditorFor(x => x.Item2.Tag.KupovinaSamoUTransportnomPakovanju);
        @Html.EditorFor(x => x.Item2.Tag.PovezaniProizvod);
        <button>Create</button>
    </form>
</div>

As you can see, i am sending Tuple<ApplicationsModel, AR.WebShop.Product<AR.TDShop.ProductCustomProperties>> as my model but for my form i use only Item2 . Problem is my inputs are named Item2.ID, Item2.Rel etc. and when i try to catch my object inside post with this: public IActionResult AddNewProduct(AR.WebShop.Product<AR.TDShop.ProductCustomProperties> product) it is not valid, but if that first part Item2 is removed, then it would work.

Is there a way to remove it inside razor?

EDIT I have tried adding this:

@{ 
    AR.WebShop.Product<AR.TDShop.ProductCustomProperties> p = Mode.item2;
}

and using it like this:

@Html.EditorFor(x => p.ID)

but it names it p.ID instead of ID

Found solution and discovered one more brilliant thing.

First of all my own answer from edit worked but all i needed to do was change my HttpPost method from AddNewProduct(AR.WebShop.Product<AR.TDShop.ProductCustomProperties> product to AddNewProduct(AR.WebShop.Product<AR.TDShop.ProductCustomProperties> p (same as i called it inside my View.

What i discovered is instead of calling each property like this all i had to do is call it like @Html.EditorFor(x => pr) and it would list all properties!

So my full working code now looks like this:

@model Tuple<ApplicationsModel, AR.WebShop.Product<AR.TDShop.ProductCustomProperties>>
@AR.WebShop.Product<AR.TDShop.ProductCustomProperties> p = Model.Item2;

<div style="padding: 20px">
    <form method="post" action="/TDShop/AddNewProduct">
        @Html.EditorFor(x => p)
        <button>Create</button>
    </form>
</div>

and catching it with AddNewProduct(AR.WebShop.Product<AR.TDShop.ProductCustomProperties> p

So clean:')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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