I am new comer in mvc. I want to pass data from dropdownlist to controller. For dropdownlist i used one partial view and added this dropdownlist in my main view. Now i want to pass selected dropdownlist value with my main view ViewModel data.
For example my main view viewmodel name ProductSubcription and my dropdownlist partial view viewmodel name
ProductSubscriptionType. Now i want to pass productsubscriptonTypeID(selected from dropdownlist) and ProductSubcription to my controller at a same time. For information in my ProductSubcription
view model has also productsubscriptonTypeID property but can not understand how to set this value and pass it to my controller.
public class ProductSubscriptionModel
{
public Guid ProductSubscriptionGuid { get; set; }
public int CustomerId { get; set; }
public string Email { get; set; }
public int SubscriptionTypeId { get; set; }
public bool Active { get; set; }
public DateTime CreatedOnUtc { get; set; }
}
public class SubscriptionTypeSelectorModel
{
public SubscriptionTypeSelectorModel()
{
AvailableSubscriptionTypes = new List<SubscriptionTypeModel>();
}
public IList<SubscriptionTypeModel> AvailableSubscriptionTypes { get; set; }
public SubscriptionTypeModel CurrentSubscriptionType { get; set; }
}
@model SubscriptionTypeSelectorModel
@if (Model.AvailableSubscriptionTypes.Count > 1)
{
var subscriptionTypes = Model.AvailableSubscriptionTypes.Select(x => new SelectListItem
{
Text = x.Name,
Value = @Url.RouteUrl("ChangeSubscriptionType", new { customerSubscriptionType = x.Id }),
Selected = x.Id.Equals(Model.CurrentSubscriptionType.Id)
});
azad_aiub
Member
69 Points
81 Posts
Pass partial view value to controller
Nov 08, 2012 12:15 PM|LINK
Dear All,
I am new comer in mvc. I want to pass data from dropdownlist to controller. For dropdownlist i used one partial view and added this dropdownlist in my main view. Now i want to pass selected dropdownlist value with my main view ViewModel data.
For example my main view viewmodel name ProductSubcription and my dropdownlist partial view viewmodel name ProductSubscriptionType. Now i want to pass productsubscriptonTypeID(selected from dropdownlist) and ProductSubcription to my controller at a same time. For information in my ProductSubcription view model has also productsubscriptonTypeID property but can not understand how to set this value and pass it to my controller.
Thanks,
ignatandrei
All-Star
135204 Points
21687 Posts
Moderator
MVP
Re: Pass partial view value to controller
Nov 08, 2012 12:17 PM|LINK
Add a property variable for the value and use Html.DropDownFor
See tutorials from http://www.asp.net/mvc
JohnLocke
Contributor
3216 Points
710 Posts
Re: Pass partial view value to controller
Nov 08, 2012 12:18 PM|LINK
Is your dropdownlist included within your form? Can you post your View and ViewModel code please.
azad_aiub
Member
69 Points
81 Posts
Re: Pass partial view value to controller
Nov 11, 2012 05:07 AM|LINK
Here is my ViewModel and View Code:
public class ProductSubscriptionModel { public Guid ProductSubscriptionGuid { get; set; } public int CustomerId { get; set; } public string Email { get; set; } public int SubscriptionTypeId { get; set; } public bool Active { get; set; } public DateTime CreatedOnUtc { get; set; } }@model ProductSubscriptionModel
@{
Layout = "~/Views/Shared/_ColumnsTwo.cshtml";
}
@using (Html.BeginForm(null, null, FormMethod.Post, new { id = "productSubscription-form" }))
{
<table border="1">
<tr>
<td colspan="2">
<h3>
Best of Breeds Products Subscription</h3>
</td>
</tr>
<tr>
<td>
Subscription Type
</td>
<td>
@Html.Action("SubscriptionTypeSelector", "ProductSubscription")
</td>
</tr>
<tr>
<td>
Email
</td>
<td>
@Html.EditorForModel(Model.Email)
</td>
</tr>
<tr>
<td colspan="2" width="100%">
<input type="submit" name="save" class="t-button" value="@T("Admin.Common.Save")" />
</td>
</tr>
</table>
}
public class SubscriptionTypeSelectorModel { public SubscriptionTypeSelectorModel() { AvailableSubscriptionTypes = new List<SubscriptionTypeModel>(); } public IList<SubscriptionTypeModel> AvailableSubscriptionTypes { get; set; } public SubscriptionTypeModel CurrentSubscriptionType { get; set; } }@model SubscriptionTypeSelectorModel
@if (Model.AvailableSubscriptionTypes.Count > 1)
{
var subscriptionTypes = Model.AvailableSubscriptionTypes.Select(x => new SelectListItem
{
Text = x.Name,
Value = @Url.RouteUrl("ChangeSubscriptionType", new { customerSubscriptionType = x.Id }),
Selected = x.Id.Equals(Model.CurrentSubscriptionType.Id)
});
@Html.DropDownList("customerSubscriptionType", subscriptionTypes,"Select Subscription Type")
}
Dropdownlist is in partial page which added inside main page. Now want to pass data when user press save button