简体   繁体   English

带有文本框ID的Html.EditorFor,可在jQuery中使用

[英]Html.EditorFor with textbox ID for use in jQuery

I am developing an ASP.NET 4.0 (.NET 4.5) web application with MVC 5 / Razor 3, Bootstrap 3.x. 我正在使用MVC 5 / Razor 3,Bootstrap 3.x开发ASP.NET 4.0(.NET 4.5)Web应用程序。 In this particular controller I make use of the Google Maps API v3. 在这个特定的控制器中,我利用了Google Maps API v3。

Basically among many other things I have two textboxes, one for Latitude the other for Longitude. 基本上,除其他外,我有两个文本框,一个用于纬度,另一个用于经度。 Then I also have a Google Map with ONE marker. 然后,我还有一个带有一个标记的Google Map。

When the user right clicks on a spot I want the current click Lat/Long to be set for the marker and update the Lat/Long textboxes and center the map there. 当用户右键单击某个地点时,我希望为标记设置当前的单击“纬度/经度”,并更新纬度/经度文本框并在此处居中放置地图。

When the user drags the marker to a new location the same thing should happen, both textboxes get updated with the new marker location. 当用户将标记拖动到新位置时,应该发生相同的事情,两个文本框都将使用新标记位置进行更新。

Other than having added the Google Map, the rest of the form is exactly the same as the Visual Studio "Add View with EF CRUD" template. 除了添加Google Map之外,其余表单与Visual Studio“使用EF CRUD添加视图”模板完全相同。

Now the problem... The textbox control code created by the VS template does not let you set a control ID that I can use with the jQuery/Javascript code to update the textboxes but all the map behaviour (marker replacement, centering, etc.) works: 现在的问题... VS模板创建的文本框控件代码不允许您设置一个控件ID,我可以将其与jQuery / Javascript代码一起使用来更新文本框,但可以更改所有地图行为(标记替换,居中等)。 )的作品:

@Html.EditorFor(model => model.longitude, new { htmlAttributes = new { @class = "form-control" } })

Somewhere in this site someone suggested another person to use TextBoxFor instead of EditorFor which appears to let you specify an ID for the textboxes. 在此站点的某个地方,有人建议其他人使用TextBoxFor而不是EditorFor,它似乎允许您为文本框指定ID。 However, that renders both textboxes in standard form which is totally discordant with the look of the other textboxes. 但是,这会以标准格式呈现两个文本框,这与其他文本框的外观完全不符。

So I then opted for another suggestion to replace that code for this: 因此,我选择了另一个建议来替换该代码:

@Html.EditorFor(model => model.latitude, null, "mylatitude", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.latitude, "", new { @class = "text-danger" })
@Html.EditorFor(model => model.latitude, null, "mylongitude", new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.longitude, "", new { @class = "text-danger" })

With this form the textboxes look like the others AND when I move the marker or right click the latitude/longitude textboxes get updated with the corresponding coordinates because I can address them from JavaScript with the IDs I stated in the EditorFor. 使用这种形式,文本框看起来与其他文本框相似,并且当我移动标记或右键单击时,纬度/经度文本框将使用相应的坐标进行更新,因为我可以使用在EditorFor中声明的ID从JavaScript对其进行寻址。

However, when I submit the form neither the latitude/longitude values on the textboxes get filled on the model, the other model values are properly filled. 但是,当我提交表单时,文本框上的纬度/经度值都不会填充到模型上,其他模型值也会正确填充。 If I revert back to the original EditorFor -where I cannot specify an ID- then the values are submitted properly. 如果我恢复为原始的EditorFor-无法指定ID-,则会正确提交值。

So there is something weird in that the textboxes get updated (proper ID) but somehow the actual textbox values filled in via Javascript are not filled into the model. 因此,有些奇怪之处在于文本框已更新(正确的ID),但通过Javascript填充的实际文本框值未以某种方式填充到模型中。

When you use the overload 使用重载时

@Html.EditorFor(model => model.latitude, null, "mylatitude", ..

The 3rd parameter is setting the name attribute to mylatitude which does not match the property name latitude so the DefaultModelBinder cannot match up the values. 第三个参数将name属性设置为与属性名称latitude不匹配的mylatitude ,因此DefaultModelBinder无法匹配这些值。

Why can't you use the ID generated @Html.EditorFor(model => model.longitude, .. which will be "longitude", or use TextBoxFor and add the relevant class names so that it is styled to look the same as the output generated by @Html.EditorFor 为什么不能使用生成的ID @Html.EditorFor(model => model.longitude, .. ,将为“经度”,或使用TextBoxFor并添加相关的类名,以便使其样式看起来与@Html.EditorFor生成的输出

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM