Click here to Skip to main content
16,019,043 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i wanna pass two models to a view . and fill both fields of these tables , there.

i did below . but in view it dose not recognize migrated model 's fields.
i mean this line: @Html.CheckBoxFor(m => m. )


C#
[HttpGet]
    public ActionResult checkout(string usernam)
    {
        ViewBag.username = usernam;

        var FeacherToTwoModel = new ViewModelX() ;

        return View(FeacherToTwoModel);

    }


    [HttpPost]
    public ActionResult checkout(ViewModelX model)
    {


        if (ModelState.IsValid)
        {


         UsersClass.InsertInOrderAndBasket(model.tbl_order.id, model.tbl_ order.id_Type,  model.tbl_ order.username,model.tbl_basket.username, model.tbl_basket.id_order);
        }
        else
        {

        }
        return View();



view :

JavaScript
.
      .
      .
     <div id="collapseFour" class="accordion-body collapse">
     <div class="accordion-inner">
     @model MyProject.Models.ViewModelX
     <p>please select a style</p>
     <label class="checkbox payment-method inline">

     @Html.CheckBoxFor(m => m.  )remember me </label>
    <label class="checkbox payment-method inline">
    @Html.CheckBoxFor(m => m. )
    online
    </label>
                       <form class="form-inline">
                           <input class="input-block span4" placeholder="Enter your coupon code here" type="text" id="couponcode">
                           <button type="submit" class="btn btn-general">register</button>
                       </form>
                   </div>
               </div>
     .
     .
     .

                           <tbody>
                           @model IEnumerable<MyProject.Models.vw_basket>
                               @foreach (var item in Model)
                               {
                                   <tr>
                                       <td class="image"><a href='../Kala/Product?NID=@item.id_product'>
                                           <img '@item.comment' alt='@item.name_product'  src="~/Content/Media/Images/product/@MyProject.Models.ProductClass.GetPicById_Product(int.Parse(item.id_Product.ToString()))" width="100"></a></td>
                                       <td class="product"><a href='../Kala/Product?NID=@item.id_ Product ' title='@item.comment'>@item.name_ Product </a> </td>
                                       <td class="stock">@item.number</td>
                                       <td class="price"><s></s> @item.cost </td>
                                       <td class="price">@(item.number*item.cost) </td>
                                   </tr>
                               }
                               <tr>

                               </tr>
                           </tbody>
                            .
                            .
                            .




calss :

C#
public class ViewModelX 
        {
            
            public tbl_order   tbl_order { get; set; }
            public tbl_basket   tbl_basket { get; set; }
           

            
        }
Posted
Updated 19-Aug-13 19:30pm
v3
Comments
Jameel VM 20-Aug-13 1:16am    
Actually what's your problem?
Jameel VM 20-Aug-13 1:17am    
wchich property you are trying to access from ViewModelX? can you post the ViewModelX class code
Member 8454063 20-Aug-13 1:34am    
i updated question . i wanna some fields in tbl_order and tbl_basket.for example m => m.tbl_order.TypeOfSpend . i used html.BeginForm before of @model MyProject.Models.ViewModelX but it dose not know fields
Jameel VM 20-Aug-13 1:51am    
what is tbl_order and tbl_basket.is it a class?
Member 8454063 20-Aug-13 2:52am    
i use codefirst. and these are in Mapping . yes these are class

1 solution

Create a new ViewModel class which contain all the property needed for that view.Your mistake is that you are putting complex type such as tbl_order and tbl_basket.This is not a good practice.Actually you need the one property in the tbl_order.ViewModels are nothing but the wrapper ie, data needed for view. So put that properties separately from the class.

Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900