如何将值从强类型视图传递到具有不同模型的强类型部分视图

时间:2017-05-16 02:56:11

标签: asp.net-mvc

我有这个强类型视图,使用ajax呈现dataTable,并且我在模式中具有这种强类型的局部视图,该模式具有与父视图不同的模型但具有公共成员。我似乎无法将特定值传递给局部视图。

父视图模型:

@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

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的值传递给partialView

1 个答案:

答案 0 :(得分: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>

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