1

我可以查看(在数据库中:右键单击表格---->查看)和模型一起进入查看页面吗?

我在页面上有一些文件想要将它们的内容发送到一个动作结果(所以我应该将模型传递给我的视图,是吗?)然后动作结果的结果是在数据库中查看。事实上我有一些 tds 正在查看想要通过 QuerySelect(在数据库中查看)填充它们的页面。

我确实喜欢下面。如果我单击第一个按钮,我的模型将为空(在 [HttpPost] 中)。如果单击第一个按钮,我的模型也是空的。

看法 :

       @model Project.Models.MyClass.WholeList


       @using (Html.BeginForm("listPlayer", "Player", FormMethod.Post))
                        {
                            @Html.AntiForgeryToken()
          @Html.TextBoxFor(m => m. PSearch.name, new { @placeholder = "* Enter name" })
           <input type="submit" value="search via name" name="Search" class="btn btn-general" />

                         }

     @model IEnumerable<Project.Models.vw_Players >

    @foreach (var item in Model)
    {
        <tr>

            <td class="stock">
                @Html.DisplayFor(modelItem => item.nam)
            </td>
            <td class="stock">
                @Html.DisplayFor(modelItem => item.cost)
            </td>
            <td class="stock">@item.number</td>
            <td class="stock">
                @Html.DisplayFor(modelItem => item.coach)
            </td>
        </tr>
    }

               @using (Html.BeginForm("listPlayer", "Player", FormMethod.Post))
                        {


                                  <select name="daystart">
                                    @for (var day = 1; day <= 31; day++)
                                    {
                                        <option value="@day">@day</option>
                                    }

                                </select>
                            <select name="monthstart">
                                <option value="01">1</option>
                                <option value="02">2</option>
                                <option value="03">3</option>
                                <option value="04">4</option>
                                <option value="05">5</option>
                                <option value="06">6</option>
                                <option value="07">7</option>
                                <option value="08">8</option>
                                <option value="09">9</option>
                                <option value="10">10</option>
                                <option value="11">11</option>
                                <option value="12">12</option>

                            </select>
                            <select name="yearstart" >
                                @for (var year = 2013; year <= Myfuc(DateTime.Now); year++)
                                {
                                    <option value="@year">@year</option>
                                }

                            </select>
               <input type="submit" value="seaech" class="btn btn-general" />
                                  }

MyClass.cs:

       public class Search
       {
    [Required(ErrorMessage = "required")]
    public string name { get; set; }
    public int daystart { get; set; }
    public int monthstart { get; set; }
    public int yearstart { get; set; }
    }

    public class WholeList
    {

        public List<Project.Models.vw_ Players > Player { get; set; }
        public Search  PSearch { get; set; }
    }

控制器 :

       [httpGet]
        public ActionResult ListPlayer()
             {

            var db = new ProjectContext();
        var objWhol = new MyClass.WholeList ();
        objWhol.Player = db.vw_Players.ToList();
        return View(objWhol);
        }


    [HttpPost]
    public ActionResult ListPlayer ( MyClass.WholeList model)
    {
        if (ModelState.IsValid )
        {
            var myvarible = MyClass.Coachlistpalayer();
             model.Player = myvarible.Where(p => p.nam.ToUpper()).ToList();
            else
            { }
            return View(model);
        }


    }
4

1 回答 1

2

你需要将军ViewModel来实现这一点。类似的东西

public class myViewModel
{
  public List<Project.Models.vw_Players> players {get; set;}
  public MyviewModel Mymodel {get; set;}
}

在视图中:

@model Project.Models.myViewModel

@foreach (var item in Model.players)
{
//** do somthing
}


@Model.Mymodel.day //** or something else

已编辑。

您还需要更改您的公众ActionResult ListPlayer()才能返回myViewModel。像这样的东西:

public ActionResult ListPlayer()
{
  MyviewModel model = new MyviewModel
{
//** define properties here
}
 List<vw_Players> players_ = db.vw_Players.ToList();

var result = new myViewModel
{
players = players_,
Mymodel = model
}

return View(result);

}
于 2013-09-15T07:39:52.777 回答