【问题标题】:Fill the Model in view and pass it controller在视图中填充模型并将其传递给控制器
【发布时间】:2015-02-04 05:11:07
【问题描述】:

我在 asp.net 中有很多问题,因为我是新手。所以我搜索了但我没有找到我的答案。

首先我的视图引擎是 aspx 不是剃须刀,这是我的主要问题。

这是视图

        <%= Html.HiddenFor(model => model.SharingPremiumHistoryID) %>
    <%= Html.HiddenFor(model => model.ItemId) %>
    <div class="group">
        <span> ارسال به </span>
        <%= Html.DropDownListFor(model => model.SharingTargetType, Model.SharingTypes) %>
    </div>
</hgroup>
<div class="newseditor">
    <div class="input-form">
        <%= Html.LabelFor(model => model.SharingTitle, "عنوان خبر") %>
        <%= Html.TextBoxFor(model => model.SharingTitle) %>
    </div>

    <div class="input-form">
        <%= Html.LabelFor(model => model.Content, "متن خبر") %>
        <%= Html.TextAreaFor(model => model.Content) %>
    </div>
    <div><input id="fileUpload" type="file" />

    </div>
                   <button name="post" type="submit" >ارسال خبر</button>

就像你一样,我有一些可以填充模型的项目。

现在我的问题是我如何通过提交按钮将此视图传递给控制器​​(没有 Ajax)。

这是控制器

  public virtual ActionResult Add()
    {
        var model = new ResturantSharingViewModel();

        model.SharingTargetType = getSharingTargetTypesSelectListItems();
        return PartialView(model);
    }

【问题讨论】:

    标签: c# asp.net-mvc


    【解决方案1】:

    您需要使用Html.BeginForm 并将HttpPost 添加到控制器操作中。

    【讨论】:

    • 谢谢@beautifulcoder,但我的视图引擎是aspx
    • @salar - 这些都不重要。 &lt;% Using(Html.BeginForm()) %&gt; 也适用于 WebForms 视图引擎。此外,[HttpPost] 是控制器/动作上的数据注释 - 它与视图引擎无关。
    【解决方案2】:

    在您的视图中显示此代码:

     <% using(Html.BeginForm("HandleForm", "Home")) %>
        <% { %>
           // your code for your page goes here
            <input type="submit" value="Submit" />
        <% } %>
    

    然后在你的控制器中有这样的代码:

      public ActionResult HandleForm()
      {
                // do controller logic here
    
                return View("FormResults");
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      相关资源
      最近更新 更多