ViewModel的参数在回发中为空

时间:2017-11-21 01:45:49

标签: c# asp.net model-view-controller viewmodel

  

我知道这个问题被多次提出并回答,但遗憾的是没有一个答案对我有帮助。

我有树POCO模型,它们是药房,地址和公司聚集在一起的ViewModel PharmacyVM:

public class PharmacyVM
{
    public Pharmacy pharmacyProp { get; set; }
    public Address addressProp { get; set; }
    public Company companyProp { get; set; }
    public int CityId { get; set; }

    public PharmacyVM()
    {
        pharmacyProp = new Pharmacy();
        addressProp = new Address();
        companyProp = new Company();
    }
}
不要嘲笑这些房产名称;在一开始,他们是正常的(地址,公司和药房)我根据这里的许多答案之一改变了它们,但是......它没有帮助:(

在Create方法的Get操作中,我尝试手动创建PharmacyVM对象并将其传递给视图:

public ActionResult Create()
{
    ViewBag.CityId = new SelectList(db.Cities, "Id", "Name");
    ViewBag.CompanyId = new SelectList(db.Companies, "Id", "Name");
    ViewBag.AddressId = new SelectList(db.Addresses, "Id", "Name");
    ViewBag.CityId = new SelectList(db.Cities, "Id", "Name");
    ViewBag.RegionId = new SelectList(db.Regions, "Id", "Name");
    ViewBag.DistrictId = new SelectList(db.Districts, "Id", "Name");
    ViewBag.MicrodistrictId = new SelectList(db.Microdistricts, "Id", "Name");
    ViewBag.StreetId = new SelectList(db.Streets, "Id", "Name");
    ViewBag.VillageId = new SelectList(db.Villages, "Id", "Name");
    var pharmacyVM = new PharmacyVM();
    return View(pharmacyVM);
}

视图中有一个标准的Razor代码:

@model MEDONET.Models.PharmacyVM
@{
    ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm("Create", "Pharmacies", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    <div class="form-horizontal">
        <h4>pharmacyProp</h4>
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

        <div class="form-group">
            @Html.LabelFor(model => model.companyProp, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("CompanyId", null, "", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.companyProp.Id, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="form-group">
            @Html.LabelFor(model => model.pharmacyProp.Name, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.pharmacyProp.Name, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.pharmacyProp.Name, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.addressProp.City, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("CityId", null, "", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CityId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.CityId, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("CityId", null, "", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.CityId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.addressProp.Village, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("VillageId", null, "", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.addressProp.VillageId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.addressProp.District, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("DistrictId", null, "", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.addressProp.DistrictId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.HiddenFor(model => model.addressProp.RegionId)
            @Html.LabelFor(model => model.addressProp.Region, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("RegionId", null, "", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.addressProp.RegionId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.addressProp.Microdistrict, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("MicrodistrictId", null, "", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.addressProp.MicrodistrictId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.addressProp.Building, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.addressProp.Building, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.addressProp.Building, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.addressProp.Street, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("MicrodistrictId", null, "", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.addressProp.MicrodistrictId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.pharmacyProp.IsGov, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <div class="checkbox">
                    @Html.EditorFor(model => model.pharmacyProp.IsGov)
                    @Html.ValidationMessageFor(model => model.pharmacyProp.IsGov, "", new { @class = "text-danger" })
                </div>
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.companyProp, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownList("companyId", null, "", htmlAttributes: new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.pharmacyProp.CompanyId, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.pharmacyProp.LargeImage, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" name="LargeImageFile" required />
            </div>
        </div>



        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </div>
    </div>
}
<div> @Html.ActionLink("Back to List", "Index")</div>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

然后在Create方法的Post动作中,我尝试创建b和c变量:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(PharmacyVM model, HttpPostedFileBase LargeImageFile)
{
    if (ModelState.IsValid)
    {
        string LargeImageFileName = Path.GetFileNameWithoutExtension(LargeImageFile.FileName);
        string LargeImageExtansion = Path.GetExtension(LargeImageFile.FileName);

        LargeImageFileName = LargeImageFileName + DateTime.Now.ToString("yymmssfff") + LargeImageExtansion;
        LargeImageFileName = Path.Combine(Server.MapPath("~/Images/Pharmacy/Banners/"), LargeImageFileName);
        int b = model.CityId;

        int c = model.addressProp.RegionId.Value;
        //Address address = new Address()
        //{
        //    Id = 12,//db.Addresses.AsEnumerable().Last().Id + 1,
        //    RegionId = PharmacyVM.Address.RegionId,
        //    DistrictId = PharmacyVM.Address.DistrictId,
        //    CityId = PharmacyVM.Address.CityId,
        //    MicrodistrictId = PharmacyVM.Address.MicrodistrictId,
        //    StreetId = PharmacyVM.Address.StreetId,
        //    VillageId = PharmacyVM.Address.VillageId,
        //    Building = PharmacyVM.Address.Building,
        //    Cab = PharmacyVM.Address.Cab
        //};
        //Address address = new Address
        //{
        //    Id = 12,//db.Addresses.AsEnumerable().Last().Id + 1,
        //    RegionId = 1,
        //    DistrictId = 1,
        //    CityId = 1,
        //    MicrodistrictId = 1,
        //    StreetId = 1,
        //    VillageId = 1,
        //    Building = "1",
        //    Cab = "1"
        //};
        //db.Addresses.Add(address);
        db.SaveChanges();
        var pharmacy = new Pharmacy
        {
            Name = model.pharmacyProp.Name,
            //AddressId = address.Id,
            IsGov = model.pharmacyProp.IsGov,
            CompanyId = model.pharmacyProp.CompanyId,
            LargeImage = "~/Images/Pharmacy/Banners/" + LargeImageFileName
        };
        LargeImageFile.SaveAs(LargeImageFileName);
        db.Pharmacies.Add(pharmacy);
        db.SaveChanges();
        ModelState.Clear();
        return RedirectToAction("Index");
    }

}

与第一个变量b不同,第二个变量c将 null ,因为addressProp属性为 null

那为什么这么奇怪?如何在不复制原始模型字段的情况下使用ViewModel?

1 个答案:

答案 0 :(得分:0)

你为什么要用

SELECT count(`P_no`) AS `ab1` FROM 
`scan_1`,`scan_2`,`scan_3` WHERE 
((`scan_1`.`work`= 'YES') OR 
(`scan_2`.`work`= 'YES') OR 
(`scan_3`.`work`= 'YES')) 

而不是

int b= model.addressProp.RegionId.Value

即使你的好词,最好的方法是删除所有的View包和 将所有View bag属性添加到PharmacyVM Class 不要使用两个属性进行视图绑定

示例

int b=model.addressProp.RegionId

More Information