在视图中填写模型并将其传递给控制器

6

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

首先,我的视图引擎是aspx而不是razor,这是我最大的问题。

以下是我的视图:

        <%= 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>

我明白您需要翻译关于IT技术的内容。以下是需要翻译的内容:

就像你说的,我有一些填充模型的物品。

现在我的问题是,如何通过提交按钮将这个视图传递到控制器(不使用Ajax)。

这是控制器:

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

        model.SharingTargetType = getSharingTargetTypesSelectListItems();
        return PartialView(model);
    }
2个回答

2
您需要使用Html.BeginForm并在控制器操作中添加HttpPost

谢谢@beautifulcoder,但我的视图引擎是aspx。 - salar
2
@salar - 这些都不重要。<% Using(Html.BeginForm()) %>同样适用于WebForms视图引擎。此外,[HttpPost]是一个数据注释,应该放在你的控制器/操作上,与视图引擎无关。 - Tommy

1
在你的视图中有这段代码:
 <% 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");
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接