【问题标题】:How to pass a value from a strongly typed view to a strongly typed partial view with a different model如何将值从强类型视图传递到具有不同模型的强类型部分视图
【发布时间】:2017-05-16 02:56:11
【问题描述】:

我有这个使用 ajax 呈现数据表的强类型视图,并且我在一个模式中拥有这个强类型部分视图,它的模型与父视图不同,但有一个共同的成员。我似乎无法将特定值传递给局部视图。

父视图模型:

@model IEnumerable<MobileNumbers>

MobileNumbers 模型:

  public class MobileNumbers : BaseEntity
{
    public long mobileNumberId { get; set; }

    [Required(ErrorMessage = "Mobile Number is Required")]
    [Display(Name = "Mobile Number")]
    [RegularExpression(@"^(09|\+639)\d{9}$", ErrorMessage = "Invalid Number Format")]
    public string mobileNumber { get; set; }

    [Display(Name = "Provider")]
    public long providerId { get; set; }

    [Display(Name = "Hired?")]
    public bool hired { get; set; }

    [Display(Name = "Resigned?")]
    public bool resigned { get; set; }

    [Display(Name = "Blocked?")]
    public bool blocked { get; set; }

    [Display(Name = "Active?")]
    public bool active { get; set; }

    [Display(Name = "Terminated?")]
    public bool terminated { get; set; }

    [Display(Name = "Walk - In Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:yyyy-MM-dd}")]
    public DateTime dateWalkIn { get; set; }

    [Display(Name = "Date Uploaded")]
    public DateTime dateUploaded { get; set; }

    [Display(Name = "Uploader")]
    public long uploadedBy { get; set; }

    public int totalCount {get; set; }

    public int totalRecords {get; set; }

    public MobileNumbers(){
        mobileNumberId = 0;
        mobileNumber = String.Empty;
        providerId = 0;
        hired = false;
        resigned = false;
        blocked = false;
        active = true;
        terminated = false;
        dateWalkIn = DateTime.Now;
        dateUploaded = DateTime.Now;
        uploadedBy = 0;

    totalCount = 0;
    totalRecords = 0;
    }

我用什么来称呼局部视图:

 @{Html.RenderPartial("~/Views/Resend/_SendMessage.cshtml",new SMSSendModels());}

局部视图模型:

@model SMSSendModels

短信发送模型

    public SMSInventory smsInventory { get; set; }
    public SmsContent SmsContent { get; set; }
    public MobileNumbers mobileNumbers {get; set;}
    public SmsInventoryUpload smsInventoryUpload { get; set; }

短信库存

 [Display(Name="SMS Txn ID")]
    public long smsId { get; set; }
    public long smsContentId { get; set; }
    [Display(Name="SMS Title")]
    public string smsContentTitle { get; set; }
    public long mobileNumberId { get; set; }
    [Required(ErrorMessage = "Mobile Number is Required")]
    [Display(Name = "Mobile Number")]
    [RegularExpression(@"^(09|\+639)\d{9}$", ErrorMessage = "Invalid Number Format")]
    public string mobileNumber { get; set; }
    [Display(Name="Comments")]
    public string comments { get; set; }
    [Display(Name="Uploader")]
    public long uploadedBy { get; set; }
    [Display(Name="Date Sent")]
    public DateTime dateSent { get; set; }
            public int totalCount {get; set; }
    public int totalRecords {get; set; }

    public SMSInventory(){
        smsId = 0;
        smsContentId = 0;
        smsContentTitle = String.Empty;
        comments = String.Empty;
        uploadedBy = 0;
        dateSent = DateTime.Now;

    totalCount = 0;
    totalRecords = 0;
    }

SMSSendModels 包含模型 MobileNumbers 和 SMSInventory,后者具有字符串 mobileNumber 的共同成员。

所以我想要做的是将模型 MobileNumbers 传递到部分视图中,或者只是将 mobileNumber 的值传递到部分视图中

【问题讨论】:

  • 请发布您的 SMSSendModels 课程
  • 请同时添加 MobileNumbers 和 SMSSendModels 模型

标签: asp.net-mvc


【解决方案1】:

您只需要制作包含两个类的 Viewmodal

 public class SMSSendModels 
{
    public MobileNumbers mobileModal { get; set; }
    public SMSInventory InventoryModal { get; set; }
}

在您的控制器内部,您可以返回 viewmodal,或者您可以传递 mobileNumber 或 SMSinventory

 SMSSendModels vm = new SMSSendModels ();

 vm.mobileModal = // assign data to
 vm.InventoryModal = // assign data to 


  // Return as what you want inside to your partial view
  return vm 

在您的视图中:

   @model WebApp.Models.SMSSendModels 

//either you can use 1st modal / 2nd modal or Both you can use


//1st modal
  <div class="col-lg-2 col-md-4 col-sm-4 col-xs-12 last-cb">
                                            @Html.LabelFor(x => x.mobileModal.fieldname, new { @class = "control-label" })
                                            @Html.TextBoxFor(mbox => mbox.mobileModal.fieldname, "", new { })
  </div>

 //2nd modal
 <div class="col-lg-3 col-md-4 col-sm-4 col-xs-12">
                                            @Html.LabelFor(x => x.InventoryModal.fieldname, new { @class = "control-label" })
                                            @*<label>Captions</label>*@
                                            @Html.TextBoxFor(m => m.InventoryModal.fieldname, new { @class = "form-control input-sm", @maxLength = "25" })
                                        </div>

我希望这会有所帮助..您仍然面临问题,您可以在下面发表评论

【讨论】:

  • 感谢您花时间回答,如果子视图在另一个控制器中,相同的解决方案是否有效?
  • 不明白,请详细说明
猜你喜欢
  • 2013-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 2013-04-07
  • 2012-09-30
  • 2013-04-23
相关资源
最近更新 更多