将表单发布到控制器后 ViewModel 内容为空
ViewModel Contents are Null after Posting Form to Controller
所以ViewModel有2组数据。
CurrentDetails 和 UpdatedDetails。两者都是相同 class 的实例,其中包含字符串和诸如此类的东西
此方法适用于我尝试过的所有其他视图和模型,但对于这个实例,当表单发布到控制器时,发现其内容(CurrentDetails 和 UpdatedDetails)均为空。
我试过将参数名称从模型更改为测试以及其他任意名称,但无济于事。
唯一有效的(但对我来说不是解决方案)是在 ViewModel 中没有 class 的实例,而只是在那里有数据(但我不明白为什么我应该被迫这样做。
这是控制器:
[HttpPost]
public ActionResult FloristProfile(MerchantFloristProfileViewModel test)
{
if (!ModelState.IsValid)
return View(test);
using (var db = new ApplicationDbContext())
{
Florist florist = db.Florists.Find(MerchantBase.FloristID);
if (Request.Form["editSubmit"] != null)
{
florist.Name = test.UpdatedDetails.Name;
florist.Website = test.UpdatedDetails.Website;
db.SaveChanges();
return RedirectToAction("FloristProfile");
}
else if (Request.Form["photoSubmit"] != null)
{
if (test.CurrentDetails.File.ContentLength > 0)
{
CloudBlobContainer container = FlowerStorage.GetCloudBlobContainer();
string blobName = String.Format("florist_{0}.jpg", Guid.NewGuid().ToString());
CloudBlockBlob photoBlob = container.GetBlockBlobReference(blobName);
photoBlob.UploadFromStream(test.CurrentDetails.File.InputStream);
florist.LogoPath = blobName;
florist.isRendering = true;
db.SaveChanges();
return RedirectToAction("FloristProfile");
}
}
}
return Content("Invalid Request");
}
查看:
@using (Html.BeginForm("FloristProfile", "Merchant", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
@Html.HiddenFor(x => x.CurrentDetails.FloristID)
@Html.HiddenFor(x => x.CurrentDetails.Name)
@Html.HiddenFor(x => x.CurrentDetails.StaffCount)
@Html.HiddenFor(x => x.CurrentDetails.StoreCount)
@Html.HiddenFor(x => x.CurrentDetails.Website)
<div class="form-group">
@Html.LabelFor(x => x.UpdatedDetails.Name, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(x => x.UpdatedDetails.Name, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.UpdatedDetails.Website, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(x => x.UpdatedDetails.Website, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" name="editSubmit" class="btn btn-success">Save</button>
</div>
</div>
}
视图模型:
public class MerchantFloristProfileViewModel
{
public class FloristProfileDetails
{
public int FloristID { get; set; }
[Required(ErrorMessage = "Please Enter a Name")]
public string Name { get; set; }
[DataType(DataType.Url)]
[Required(ErrorMessage = "Please Enter a Website")]
public string Website { get; set; }
public int StoreCount { get; set; }
public int StaffCount { get; set; }
// For Picture Upload
public HttpPostedFileBase File { get; set; }
}
public FloristProfileDetails CurrentDetails;
public FloristProfileDetails UpdatedDetails;
}
你的 MerchantFloristProfileViewModel
模型中的 CurrentDetails
和 UpdatedDetails
都是字段,而不是属性(没有 getter/setter)所以 DefaultModelBinder
不能 设置 值。将它们更改为
public FloristProfileDetails CurrentDetails { get; set; }
public FloristProfileDetails UpdatedDetails { get; set; }
但是您不应该将所有额外的数据发送到视图,然后再原样发送回去。除了额外的开销之外,任何恶意用户都可能更改隐藏字段中的值,从而导致您的应用程序失败。如果您需要在 POST 方法
中再次从存储库中获取原始文件
所以ViewModel有2组数据。 CurrentDetails 和 UpdatedDetails。两者都是相同 class 的实例,其中包含字符串和诸如此类的东西
此方法适用于我尝试过的所有其他视图和模型,但对于这个实例,当表单发布到控制器时,发现其内容(CurrentDetails 和 UpdatedDetails)均为空。
我试过将参数名称从模型更改为测试以及其他任意名称,但无济于事。
唯一有效的(但对我来说不是解决方案)是在 ViewModel 中没有 class 的实例,而只是在那里有数据(但我不明白为什么我应该被迫这样做。
这是控制器:
[HttpPost]
public ActionResult FloristProfile(MerchantFloristProfileViewModel test)
{
if (!ModelState.IsValid)
return View(test);
using (var db = new ApplicationDbContext())
{
Florist florist = db.Florists.Find(MerchantBase.FloristID);
if (Request.Form["editSubmit"] != null)
{
florist.Name = test.UpdatedDetails.Name;
florist.Website = test.UpdatedDetails.Website;
db.SaveChanges();
return RedirectToAction("FloristProfile");
}
else if (Request.Form["photoSubmit"] != null)
{
if (test.CurrentDetails.File.ContentLength > 0)
{
CloudBlobContainer container = FlowerStorage.GetCloudBlobContainer();
string blobName = String.Format("florist_{0}.jpg", Guid.NewGuid().ToString());
CloudBlockBlob photoBlob = container.GetBlockBlobReference(blobName);
photoBlob.UploadFromStream(test.CurrentDetails.File.InputStream);
florist.LogoPath = blobName;
florist.isRendering = true;
db.SaveChanges();
return RedirectToAction("FloristProfile");
}
}
}
return Content("Invalid Request");
}
查看:
@using (Html.BeginForm("FloristProfile", "Merchant", FormMethod.Post, new { @class = "form-horizontal" }))
{
@Html.ValidationSummary(false, "", new { @class = "text-danger" })
@Html.HiddenFor(x => x.CurrentDetails.FloristID)
@Html.HiddenFor(x => x.CurrentDetails.Name)
@Html.HiddenFor(x => x.CurrentDetails.StaffCount)
@Html.HiddenFor(x => x.CurrentDetails.StoreCount)
@Html.HiddenFor(x => x.CurrentDetails.Website)
<div class="form-group">
@Html.LabelFor(x => x.UpdatedDetails.Name, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(x => x.UpdatedDetails.Name, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(x => x.UpdatedDetails.Website, new { @class = "col-sm-2 control-label" })
<div class="col-sm-10">
@Html.TextBoxFor(x => x.UpdatedDetails.Website, new { @class = "form-control" })
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" name="editSubmit" class="btn btn-success">Save</button>
</div>
</div>
}
视图模型:
public class MerchantFloristProfileViewModel
{
public class FloristProfileDetails
{
public int FloristID { get; set; }
[Required(ErrorMessage = "Please Enter a Name")]
public string Name { get; set; }
[DataType(DataType.Url)]
[Required(ErrorMessage = "Please Enter a Website")]
public string Website { get; set; }
public int StoreCount { get; set; }
public int StaffCount { get; set; }
// For Picture Upload
public HttpPostedFileBase File { get; set; }
}
public FloristProfileDetails CurrentDetails;
public FloristProfileDetails UpdatedDetails;
}
你的 MerchantFloristProfileViewModel
模型中的 CurrentDetails
和 UpdatedDetails
都是字段,而不是属性(没有 getter/setter)所以 DefaultModelBinder
不能 设置 值。将它们更改为
public FloristProfileDetails CurrentDetails { get; set; }
public FloristProfileDetails UpdatedDetails { get; set; }
但是您不应该将所有额外的数据发送到视图,然后再原样发送回去。除了额外的开销之外,任何恶意用户都可能更改隐藏字段中的值,从而导致您的应用程序失败。如果您需要在 POST 方法
中再次从存储库中获取原始文件