通过MVC的中继后的值MVC

由网友(空白答卷)分享简介:表 - @using IEnumerable@foreach(var item in Model){
表 -

@using IEnumerable<Myapplication.Models.CardModel>
    @foreach(var item in Model)
    {

          <form method="post" action="/Upload/EditCard/?cardID=@item.cardID" enctype="multipart/form-data">
                                    <h3>
                                        Change Title-
                                    </h3>
                                    <div class="display-field">
                                        @Html.HiddenFor(m => item.cardTitle)
                                        @Html.TextBoxFor(cardTitle => item.cardTitle)
                                    </div>
                                     <img src="@item.cardFilePath" />
                                    <input type="submit">
                          </form>
    }

方法 -  [HttpPost]

Method- [HttpPost]

public void EditCard(CardModel card, HttpPostedFileBase file) {}

凡在形式,我通过这种形式发送的值,而 cardID 形式的URL参数发送。

Where in form I am sending values through this form, and cardID is sent in form's url parameter.

有关其他的价值,比如 cardTitle 即将空的 EditCard 方法。

For other value like cardTitle is coming null in EditCard Method.

如何使用中继器,我得到这个值?

How do I get this value using repeater?

然而,当数据不是的IEnumerable 类型,那么我可以通过形式直接发送值 -

However when data is not IEnumerable type , then I was able to send value through form directly as-

@using Myapplication.Models.CardModel

@using Myapplication.Models.CardModel

      <form method="post" action="/Upload/EditCard/?cardID=@Model.cardID" enctype="multipart/form-data">
                                <h3>
                                    Change Title-
                                </h3>
                                <div class="display-field">
                                    @Html.HiddenFor(m => m.cardTitle)
                                    @Html.TextBoxFor(m=> m.cardTitle)
                                </div>
                                 <img src="@Model.cardFilePath" />
                                    <input type="submit">
                      </form>
}

但如果直放站值不来的方法。 当我更改标题,或任何其他的值,这些值来旧的。

从图片 -

和服务器code -

你可以从1号的画面是,表格是可编辑从列表中的一个记录看。当我改变的东西在IE中的标题为 - > 第二上传第二Uploadsssss

As you can see from 1st picture that, Form is editable for one record from the list. As soon as I change something in i.e. Title to --> 2nd Upload to 2nd Uploadsssss

那么这个值是在服务器端空。我的意思是这种形式不发送它的值。

Then this values is null at server side. I mean this form don't send values of it.

注意 -

不过,我可以通过参数的URL发送的值。但是,如果我不改变类似标题或 aboutCard 模型值。形式不断发送只有那些已经在默认情况下,而形式呈现的值。

However I can send values in URL through parameters. But if I do change in something like Title or aboutCard model value. The form keeps sending only those values which has come by default while the form was rendered.

推荐答案

我不知道这是否有助于解决您的问题。我想这和它的作品真的很好。

I dont know if this helps in solving your problem. I tried this and it works really well.

示范

public class CardFileModel
{
    public int CardId { get; set; }
    public string Name { get; set; }
    public HttpPostedFileBase File { get; set; }
}

索引操作

public ActionResult Index()
{
   List<CardFileModel> cards = new List<CardFileModel>
   {
       new CardFileModel{ CardId =1 , Name="Card1"  },
       new CardFileModel{ CardId =2 , Name="Card2"  }
   };

   return View(cards);
}

首页查看

@model List<MVC3Stack.Models.CardFileModel>
@{
    ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
@for (int i = 0; i < Model.Count; i++)
{
    using (Html.BeginForm("Index", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
    <div>
        <div>
            @Html.TextBoxFor(x => x[i].CardId)
        </div>
        <div>
            @Html.TextBoxFor(x => x[i].Name)
        </div>
        @Html.TextBoxFor(x => x[i].File, new { type = "file" })
        <input type="submit" value="submit" id="submit" />
    </div>
    }
}

指数发布操作

index post action

[HttpPost]
public ActionResult Index(List<CardFileModel> card)
{
    //Access your card file from the list. 
    var name = card.FirstOrDefault().Name;
    return View();
}

和这里的结果在快速手表

And here are the results in the quick watch

希望这有助于。

阅读全文

相关推荐

最新文章