从视图向控制器传递.Net Core中的多个复杂对象

时间:2019-09-10 20:14:25

标签: c# forms asp.net-core asp.net-core-mvc

我需要发布一个表单,该表单应该发送一个包含其他对象和对象列表的对象,该对象可能还包含其他对象。

示例:

class Store{
    string Title;
    string Image;
    List<Classification> Classifications;
    Branch MainBranch;
    List<ContactInfo> StoreContact;
}

class Classification {
    int ID;
    string Title_AR;
    string Title_EN;
}

class Branch{
    string Title;
    Address MainAddress;
    List<ContactInfo> BranchContact;
}

class ContactInfo{
    ContactInfoType  ContactType;
    string  ContactValue;
}

class Address{
    City city;
    Locality locality;
    SubLocality sublocality;
}

我在视图中添加了具有所有必填字段的表单,并将其发布到控制器,但没有将数据解析为对象存储中的复杂对象和列表(我知道我仍然需要编辑输入的命名HTML中,但也无法正常工作

这是“查看页面”中的表单

<form  asp-controller="Onboarding" asp-action="SaveBranch" method="post" 
  role="form" class="form-horizontal" id="wizard-validation">
    <div class="form-group">
        <div class="col-md-1 store-name">
            <label class="control-label">أسم المتجر</label>
        </div>
        <div class="col-md-4 col-md-offset-1 store-name">
            <input type="text" class="form-control" id="StoreTitle" 
              name="StoreTitle" value="البستان" />
        </div>
        <div class="col-md-1">
            <label class="control-label store-logo-lable">اضف<br>لوجو</label>        
        </div>
        <div class="col-md-2">
            <div class="store-logo-upload pull-right">
                <i class="fa fa-home"></i>
            </div>
            <input class="hidden" type="file" id="file-simple" />
        </div>
    </div>
    <div class="form-group sup">
        <div class="col-md-1">
            <label class="control-label">جهات الاتصال</label>
        </div>
        <div class="form-group col-md-11 form-inline sup pull-left-i " id="klon1">
            <div class="form-group col-md-2 no-pad-lft">
                <select class="form-control select" style="display: none;" name="ContactType">
                    <option>تليفون</option>
                    <option>الايميل</option>
                 </select>
            </div>
            <div class="form-group">
                <input type="text" class="form-control SearchInput" name="Name" />
            </div>
            <div class="form-group CloneBtn">
                <button type="button" class="btn btn-default">
                    <i class="fa fa-plus"></i>
                </button>
            </div>
        </div>
    </div>
    <div class="form-group">
        <div class="col-md-1">
            <label class="control-label">التصنيف</label>
        </div>
        <div class="col-md-6">
            <select multiple class="form-control select" data-live-search="true" 
              title="اختر التصنيف"></select>
        </div>
    </div>
    <h4 class="add-branch-tit">أضافه فرع</h4>
    <hr class="col-md-8 col-md-offset-4">
    <div class="form-group sup">
        <div class="col-md-1">
            <label class="control-label">اسم الفرع</label>
        </div>
        <div class="col-md-4">
            <input type="text" class="form-control" name="Name" />
        </div>
    </div>
    <div class="form-group sup">
        <div class="col-md-1 sys-address-label">
            <label class="control-label">العنوان</label>
        </div>
        <div class="col-md-7">
            <div class="row sys-address">
                <div class="col-md-3">
                    <label class="SubLabel ">الدولة</label>
                    <select class="form-control select">
                        <option>مصر</option>
                        <option>فلسطين</option>
                        <option>السعودية</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="SubLabel ">المحافظة</label>
                    <select class="form-control select">
                        <option>القاهرة</option>
                        <option>فلسطين</option>
                        <option>السعودية</option>
                    </select>
                </div>
                <div class="col-md-3">
                    <label class="SubLabel ">المدينة</label>
                    <select class="form-control select">
                        <option>مدينة نصر</option>
                        <option>فلسطين</option>
                        <option>السعودية</option>
                    </select>
                </div>
                <div class="col-md-3 sup">
                    <label class="SubLabel ">المنطقة</label>
                    <select class="form-control select">
                        <option>عباس العقاد</option>
                        <option>فلسطين</option>
                        <option>السعودية</option>
                    </select>
                </div>
                <div class="col-md-12">
                    <label class="SubLabel ">الشارع</label>
                    <input type="text" class="form-control" name="Name" />
                </div>
            </div>
        </div>
    </div>
    <div class="form-group sup">
        <div class="col-md-1">
            <label class="control-label">جهات الاتصال</label>
        </div>
        <div class="form-group col-md-11 form-inline sup pull-left-i" id="klon1">
            <div class="form-group col-md-2 no-pad-lft">
                <select class="form-control select" style="display: none;">
                    <option>تليفون</option>
                    <option>الايميل</option>
                </select>
            </div>
            <div class="form-group">
                <input type="text" class="form-control SearchInput" name="Name" />
            </div>
            <div class="form-group CloneBtn">
                <button type="button" class="btn btn-default">
                    <i class="fa fa-plus"></i>
                </button>
            </div>
        </div>
    </div>
    <hr>
    <input type="submit" value="الموردون"/>
</form>

这是控制器:

public IActionResult SaveBranch(Store store)
{
    var isSucceed = Store.AddStore(store);
    return isSucceed ? AddSupplier() :  AddBranch("Error");
}

我希望实际输出的对象为Store,其中包含联系信息列表,而MainBranch以及其中的另一个联系人信息列表和地址。

结果仅是所提供商店的主要信息,其余为空。

1 个答案:

答案 0 :(得分:1)

您需要为输入设置正确的asp-for属性或名称属性,然后选择。如果有列表,则还需要指定其索引,例如asp-for="StoreContact[0].ContactValue"

一个简单的演示如下所示:

@model Store
<form asp-controller="Onboarding" asp-action="SaveBranch" method="post" >
    <div asp-validation-summary="ModelOnly" class="text-danger"></div>
    <div class="form-group">
        <label asp-for="Title" class="control-label"></label>
        <input asp-for="Title" class="form-control" />
    </div>
    <div class="form-group">
        <label asp-for="Image" class="control-label"></label>
        <input asp-for="Image" class="form-control" />
    </div>      
    <div class="form-group">
        <label class="control-label">StoreContact1 ContactValue</label>
        <input asp-for="StoreContact[0].ContactValue" class="form-control" />

    </div>
    <div class="form-group">
        <label class="control-label">StoreContact2 ContactValue</label>
        <input asp-for="StoreContact[1].ContactValue" class="form-control" />

    </div>
   <div class="form-group">
        <label class="control-label">MainBranch Title</label>
        <input asp-for="MainBranch.Title" class="form-control" />

    </div>
    <div class="form-group">
        <label class="control-label">MainBranch City</label>
        <select asp-for="MainBranch.MainAddress.City.CityName" class="form-control" >
          <option>...</option>
        </select>

    </div>
    <div class="form-group">
        <input type="submit" value="Create" class="btn btn-primary" />
    </div>
</form>