如何在创建其他内部模型时进行搜索

问题描述:

我有这种局部视图来搜索发布者,但是当我以另一种形式与另一个动作结果一起使用它时,它会响应其他像这样的内容。如何在创建其他内部模型时进行搜索

这个观点是我的行动在未来一个

@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

你的问题是你在另一个表单中嵌入一个表单。

最好的方法是使用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($(“”,{value:response.Id,text:response.Name})); 引用正确的列(选择列表),只需确保以下行: $名称 –

+0

没问题!如果这解决了你的问题,请考虑标记为答案,祝你有美好的一天:) –