2016-11-09 68 views
0

我有這種局部視圖來搜索發佈者,但是當我以另一種形式與另一個動作結果一起使用它時,它會響應其他像這樣的內容。如何在創建其他內部模型時進行搜索

這個觀點是我的行動在未來一個

@using (Html.BeginForm("publisherslist",FormMethod.Get)) 
      { 
      <input type="text" id="q" name="q"/> 
      <input type="submit" /> 
      } 
       @foreach (var i in Model) 
     { 
     @Publisher.Name 
      <br /> 
      } 

,我呈現在另一種觀點

  @model ELibrary_Search_Engine.BookModels.Book 
      @using (Html.BeginForm("Create", null, FormMethod.Post, new {enctype= "multipart/form-data" })) 
{ 
     <div class="form-horizontal"> 
      <div class="form-group"> 
       @Html.LabelFor(model => model.Name, new { @class = "control-label col-md-2" }) 
        <div class="col-md-10"> 
         @Html.EditorFor(model => model.Name) 
         @Html.ValidationMessageFor(model => model.Name) 
        </div> 
       </div> 

         <div class="form-group"> 
        @Html.LabelFor(model => model.CategoryID, "CategoryID", new { @class = "control-label col-md-2" }) 


     <div class="col-md-10"> 
          @Html.DropDownList("CategoryID", String.Empty) 
          @Html.ValidationMessageFor(model => model.CategoryID) 
         </div> 
        </div> 
       <input type="file" id="file" name="file" /> 
        <br /> 
        <label>Choose Publisher:</label> 
         <br /> 
         @Html.Action("publisherslist") 
        <input type="submit" value="Create" /> 
        </div> 
        } 

我想啓用出版商搜索的局部視圖

+0

您是否必須按照此方法搜索發佈者?我的意思是我們可以建議改進嗎?請列出所提供的'views',這樣我們就不會做出錯誤的假設。 'Model'在'publisherslist'視圖中返回什麼? 「Html.RenderPartial(」發佈者「,我)是做什麼的? – Searching

+0

我糾正了你說的對不起,因爲它很糟糕 我想第一個formmethod發佈者列表在創建視圖中呈現 –

+0

NP沒關係。看起來您正嘗試在創建表單中搜索發佈商,然後將其與其他詳細信息一起提交。在當前的設置中,它可能不起作用,因爲搜索(即使它是正確的)只返回純文本。你有沒有考慮過使用'ajax.get'來代替搜索發佈者。 'Book'模型是否有'PublisherName'屬性來綁定? – Searching

回答

0

你的問題是你在另一個表單中嵌入一個表單。

最好的方法是使用JQuery根據搜索字符串填充發布者列表。

在你的HTML:

<input type="text" id="q" name="q"/> 
<input type="button" onclick="getPublishers" /> 

<select id="publisher-list"> 
</select> 

在你的JavaScript:

function getPublishers() 
{ 
    //Cache your query string. 
    var queryString = $("#q").val(); 
    //URL of your searching action. 
    var url = "/ControllerName/publisherslist?queryString=" + queryString; 
    //Clear the current select list. 
    $("#publisher-list").empty(); 

    //Pull the list of publishers. 
    $.ajax(
     url : url, 
     success : function(response){ 
     //Insert the publishers into the select list, make sure to reference the value you want to be displayed from the JSON object 
      $("#publisher-list").append($("<option/>", {value: response.Id, text: response.Name})); 
     }); 
} 

在C#

public JsonResult(var queryString) 
{ 
    //TODO: Search logic and return objects in JSON format. 
} 

我assumin g你的搜索方法正在工作,你只需要將返回的數據格式化成一個JSON對象,這個例子應該可以工作 - 有一些小竅門。

+0

我怎麼可以插入的發佈商選擇列表 這是我第一次使用JSON 工作,並感謝你很多 –

+0

我不知道你的搜索如何工作的,但有些事沿着線: VAR出版商= _db。 Publishers.Where(pub => pub.Name.Contains(queryString).ToList(); return Json(publishers); 更改模型和參數名稱以在適當的位置匹配您的javascript,然後將返回的值注入到(「#publisher-list」)。append($(「

+0

沒問題!如果這解決了你的問題,請考慮標記爲答案,祝你有美好的一天:) –

相關問題