0

モデル プロパティ Name の ID を MVC4 アプリケーションの Controller クラスに送信する方法

  public class{
public Name{get;set;}
}

そのプロパティ更新のIDを使用して名前にアクセスする場合:ここで、実行時にjqueryを使用して名前を変更する場合、変更された名前IDをコントローラークラスに送信します

更新日: これは私の見解です

<script type="text/javascript">

$(function () {

    $('.editor input').blur(function () {
        $(this).hide();
        $(this).closest('p').find('label').html($(this).val()).show();

    });

    $('.editor label').click(function () {
        $(this).hide();
        $(this).closest('p').find('input').show();

    });

});

@using (Html.BeginForm("Homepage", "Home", FormMethod.Post))
{ 
     <div class="editor">

    <p>
        @Html.LabelFor(x => x.Name, Model.Name)
        @Html.EditorFor(x => x.Name)
       <input type="submit" value="OK"  />
       </p>

     <p>
        @Html.LabelFor(x => x.Company, Model.Company)
        @Html.EditorFor(x => x.Company)
      <input type="submit" value="OK" />
    </p>
     <p>
        @Html.LabelFor(x => x.City, Model.City)
        @Html.EditorFor(x => x.City)
         <input type="submit" value="OK" />
    </p>
</div> 
<input type="submit" value="OK"   />
}

これは私のモデルです

 public class Details
{

    public string Name
    { get; set; }


    public string Company
    { get; set; }


    public string City
    { get; set; }


}

これは私のコントローラーメソッドです

 public ActionResult Homepage(Details d)
    {
        d.Name = "Rakesh";
        d.Company = "TCS";
        d.City = "DElhi";
        return View(d);
    }

    [HttpPost, ActionName("Homepage")]
    public ActionResult Indexof(Details d)
    {
      return View(d);
    }

ここでデータを編集してコントローラーに送信していますが、問題は、たとえば Rakesh をクリックして名前を変更し、ボタンを 2 回クリックする必要があり、変更されたデータのみがコントローラー クラスに送信されることです。

4

1 に答える 1

1

モデル:

public class SomeModel {
    public string Name { get; set; }
}

コントローラ:

[HttpPost]
public ActionResult YourAction( SomeModel m )
{
    if( ModelState.IsValid )
    {
        // use model
        var name = m.Name;

        return RedirectToAction( "Index", "Home" );
    }
    return View( m );
}

これが必要なものでない場合は、あなたが話しているこの「ID」が何であるかを明確にしてください。

于 2012-07-04T11:32:12.603 に答える