我想设计通用的模型绑定,使用一个公共的局部视图和其他多个基于不同模型绑定的模型类型的局部视图。
我有一个基本的对话模型和两个派生模型。
public class DialogModel
{
public int DialogType { get; set; }
public string ViewName { get; set; }
}
public class DialogModel1: DialogModel
{
}
public class DialogModel2: DialogModel
{
}我有一个操作方法,我根据模型类型发布JSON数据。
public IActionResult OpenDialog(DialogModel model)
{
string view = string.Empty;
switch (model.DialogType)
{
case 1:
model.View = "MyDialogPartialView1";
DialogModel1 model1 = (DialogModel1)model;
//mode1 properties set;
view = this.RenderViewAsync("CommonDialogPartialView", model1, true).Result;
break;
case 2:
model.View = "MyDialogPartialView2";
DialogModel2 model2 = (DialogModel2)model;
//mode2 properties set;
view = this.RenderViewAsync("CommonDialogPartialView", model2, true).Result;
break;
default:
break;
}
return new JsonResult(new { view });
}我根据对话框类型设置局部视图的视图名称,并转换为相关模型,呈现公共局部视图"CommonDialogPartialView“,并返回响应文本。
我将此响应文本附加到客户端。
我常用的局部视图HTML。
@model ChurchPharmacy3.Models.DialogModel
<div class="modal-dialog modal-dialog-centered">
<div class="modal-content" id="popup_body">
<div class="modal-header bg-info">
<h5 class="modal-title" id="exampleModalLongTitle">@Model.Title</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
@await Html.PartialAsync(Model.View)
<!--This partial view can be "MyDialogPartialView1" or "MyDialogPartialView2" -->
</div>
<div class="modal-footer bg-light">
<button type="button" class="btn btn-secondary">Cancel</button>
<button type="button" class="btn btn-primary">Ok</button>
</div>
</div>
</div>这个公共局部视图获取DialogModel的模型,我正在根据视图名称从公共局部视图渲染另一个局部视图。
下面是对话框类型1的部分视图。
@model ChurchPharmacy3.Models.DialogModel1
<!--Model of DialogModel1-->对话框类型2的分部视图也是如此。
@model ChurchPharmacy3.Models.DialogModel2
<!--Model of DialogModel1-->当我将模型转换为DialogModel1或DialogModel2时,它抛出异常。
发布于 2019-12-11 19:18:52
我已经使用了IModelBinder并且能够解决上面的问题。
以下是上述问题的解决方案。
public class DialogModelBinder : IModelBinder
{
public Task BindModelAsync(ModelBindingContext bindingContext)
{
if (bindingContext == null)
throw new ArgumentNullException(nameof(bindingContext));
JObject jObject = null;
using (StreamReader reader = new StreamReader(bindingContext.ActionContext.HttpContext.Request.Body))
{
using (var jsonReader = new JsonTextReader(reader))
{
jObject = JObject.LoadAsync(jsonReader).Result;
}
}
var modelType = (int)jObject.SelectToken("dialogType");
if (modelType == (byte)DialogType.Dialog1)
{
var obj = DeserializeObject<Dialog1>(jObject.ToString());
bindingContext.Result = ModelBindingResult.Success(obj);
}
if (modelType == (byte)DialogType.Dialog1)
{
var obj = DeserializeObject<Dialog2>(jObject.ToString());
bindingContext.Result = ModelBindingResult.Success(obj);
}
else
{
var obj = DeserializeObject<DialogModel>(jObject.ToString());
bindingContext.Result = ModelBindingResult.Success(obj);
}
return Task.CompletedTask;
}
private static T DeserializeObject<T>(string json)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json, new Newtonsoft.Json.JsonSerializerSettings
{
TypeNameHandling = Newtonsoft.Json.TypeNameHandling.Auto
});
}
}https://stackoverflow.com/questions/59282154
复制相似问题