0

ASP.NET MVC 3を使用するWebアプリケーションでは、パラメーターとして初期化されたプロパティを持つモデルをコントローラーから部分ビューに渡します。

ビューには単一のテキストボックスを含むダイアログが表示され、送信時に開始コントローラーでアクションが実行されます(アクションはパラメーターと同じモデルタイプを取ります)。
問題は、この時点で、テキストボックスフィールドに関連するプロパティのみが値を持ち、その値はユーザーによって挿入され、他のすべてのプロパティは、ビューに適切な値があったとしてもnullであるということです。

送信ボタンがクリックされた後、プロパティをビューからコントローラーに保持するにはどうすればよいですか?

編集(追加されたコード):

 //----------   This method in the controller call the Partial View and pass the model  --------
[HttpPost]
public PartialViewResult GetAddCustomFormerClubDialog()
    {
        var order = GetCurrentOrder();
        //Order has here all properties initialized

        var dialogModel = new dialogModel<Order> { Entity = order, ControllerAddEntityActionName = "SelectOrder"};

        return PartialView("Dialogs/AddOrder", dialogModel);
    }



//-----------------   Here the Partial View   -----------------------------------
@model FifaTMS.TMS.Presentation.Model.Wizard.WizardDialogModel<Club>

<div>
@using (Ajax.BeginForm(Model.ControllerAddEntityActionName, "Orders", new AjaxOptions { HttpMethod = "POST"}))
{
    @Html.LabelFor(a => a.Entity.Name)
    @Html.TextBoxFor(a => a.Entity.Name, new { @class = "isrequired", style="width: 250px;" })
 }
</div>



//--------  Here the method from the view (in the same controller as the first code portion)  -----
 [HttpPost]
 public JsonResult SelectOrder(dialogModel<Order> OrderModel)
    {
       var order= OrderModel.Entity;
      // But order has only the property Name set (in the view)

     ...
    }
4

1 に答える 1

1

次のように、必要なプロパティごとに非表示フィールドを追加するだけで、問題を解決できました。

@Html.HiddenFor(p => p.Entity.OrderId, new { id = "OrderId" })

これは、PartialViewからモデルの新しいインスタンスが作成され、コントローラーに送信されるためです。したがって、フォームで設定されたプロパティのみが取得されます(私の場合、唯一のフィールドは、PartialViewのTextBoxに関連するOrderNameでした)。

于 2012-10-05T14:29:46.163 に答える