2016-12-18 17 views
1

ビューコードの引数strをコントローラに渡すことができません。それはヌルとして終わる。私はそれを ""として渡したい。アクションリンク引数がコントローラに正しく渡されない

助けが必要ですか?

コントローラコード:

public ActionResult Index(string str, string sortOrder) 
{ 

    if (str == "") 
     return View(db.Ports.OrderByDescending(model => model.PurchaseDate)); 

    ViewBag.NameSortParm = String.IsNullOrEmpty(sortOrder) ? "name_desc" : ""; 
    ViewBag.DateSortParm = sortOrder == "Date" ? "date_desc" : "Date"; 
    var investments = from s in context.Investments 
        select s; 
    switch (sortOrder) 
    { 
     case "name_desc": 
      investments = investments.OrderByDescending(s => s.LastName); 
      break; 
     case "Date": 
      investments = investments.OrderBy(s => s.PurchaseDate); 
      break; 
     case "date_desc": 
      investments = investments.OrderByDescending(s => s.PurchaseDate); 
      break; 
     default: 
      investments = investments.OrderBy(s => s.LastName); 
      break; 
    } 
    return View(investments.ToList()); 
} 

コードの表示:

@model StockHoldings.Models.Investments 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 
<head> 
    <script src="http://code.jquery.com/jquery-1.10.1.min.js" type='text/javascript'></script> 
    @*<script> src='http://code.jquery.com/jquery-latest.min.js' </script>*@  
</head> 

@using (Html.BeginForm()) 
{ 
    @Html.AntiForgeryToken() 

    <div class="form-horizontal"> 
     <h4>Investments</h4> 
     <hr /> 
     @Html.ValidationSummary(true, "", new { @class = "text-danger" }) 
     <div class="form-group"> 
      @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.DropDownListFor(model => model.LastName, (SelectList)ViewBag.LastNames) 

       @*@Html.EditorFor(model => model.LastName, new { htmlAttributes = new { @class = "form-control" } })*@ 
       @Html.ValidationMessageFor(model => model.LastName, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Fund, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.Fund, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Fund, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.PurchaseDate, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.EditorFor(model => model.PurchaseDate, new { htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.PurchaseDate, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.Shares, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @*@Html.EditorFor(model => model.Shares, new { htmlAttributes = new { @class = "form-control" } })*@ 
       @Html.TextBoxFor(model => model.Shares, new { id = "sharesID", name = "Shares", htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.Shares, "", new { @class = "text-danger" }) 
      </div> 
     </div> 

     <div class="form-group"> 
      @Html.LabelFor(model => model.PurchasePrice, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @*@Html.EditorFor(model => model.PurchasePrice, new { htmlAttributes = new { @class = "form-control" } })*@ 
       @Html.TextBoxFor(model => model.PurchasePrice, new { id = "purchasepriceID", name = "PurchasePrice", htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.PurchasePrice, "", new { @class = "text-danger" }) 
      </div> 
     </div> 


     @*<div class="form-group"> 
      @Html.LabelFor(model => model.PurchaseAmount, htmlAttributes: new { @class = "control-label col-md-2" }) 
      <div class="col-md-10"> 
       @Html.TextBoxFor(model => model.PurchaseAmount, new { id = "purchaseamountID", name = "PurchaseAmount", htmlAttributes = new { @class = "form-control" } }) 
       @Html.ValidationMessageFor(model => model.PurchaseAmount, "", new { @class = "text-danger" }) 
      </div> 
     </div> 
    *@ 

     <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 

ここのリンクです:

@Html.ActionLink("Investments", "Index", "Investments", new { str = "", sortOrder = "Date" }, null) 

</div> 

答えて

0

あなたはアクションメソッドに引数を変更することができ、文字列strがオプションのパラメータであることを確認してください public ActionResultインデックス(文字列sortOrder、文字列str = "")

関連する問題