2

With an existing MVC based application, There is a Product view which has ViewModel as ProductViewModel, This ViewModel has all basic properties which Product can have like Product Name, Type, Price, etc....

With this Product Creation process on this view, Objective is to associate list of customers with Product (which is not mendatory).

To retrieve Customer list for the user to select those associated customers, Currently on a button click, there is ajax call (getCustomers) which requests controller action to get list & show it within a table along with checkbox, this html created on the fly in jQuery Ajax call,

With current implementation, Selected Ids are not getting populated in view model while making POST action of the Product View.

Objective is to get selected Customer Ids, along with Product details into ViewModel.

Is there any way to do so?

4

1 回答 1

0

我会尽量用我能理解的最好的方式来回答。因此,假设您已经创建了带有复选框的“表”,我想它看起来像这样:

<form>
<table>
  <tr><td><input type='checkbox' value='1' name='myCheckbox' />Some text</td></tr>
  <tr><td><input type='checkbox' value='2' name='myCheckbox' />Some text</td></tr>
  <tr><td><input type='checkbox' value='3' name='myCheckbox' />Some text</td></tr>
  <tr><td><input type='checkbox' value='4' name='myCheckbox' />Some text</td></tr>
</table>
<input type="submit" />
</form>

在您的 ProductModel 类中:

public class ProductModel
{
   // Define the property here.
   public int[] MyCheckbox { get; set; }

   // The constructor where you do the initialization
   public void ProductModel(int[] myCheckbox)
   {
      MyCheckbox = myCheckbox;
   }
}

在您的控制器方法中:

[HttpPost]
public ActionResult SomeMethod(ProductModel productModel)
{
    foreach(int value in productModel.MyCheckbox)
    {
        // Your code here..
    }
}

您将在控制器方法中将复选框的值作为数组获取。

于 2013-07-01T12:47:58.257 回答